Values are keep changing when we put lookup in datagrid and scroll the grid

Nov 16, 2012 at 8:53 AM

Hi,

Good Day!

 

We would like to get some help regarding slivercrmlookup. :)

We got the assignment to develop silverlight 4 DataGrid with lookup, picklist, etc....

So, we tried to use your silvercrmlookup. It works quite well.

But, we found couple of problems. Maybe, we are not that familiar with silverlight yet.

The first thing what we found is the value of silvercrmlookup's value is keep changing if we scroll the DataGrid up and down.

The second thing is we change the value of silvercrmlookup. But, the value change back to original value when we tab out.

ItemsSource for DataGrid is contact.

DataGrid's DataTemplate:

<DataTemplate>
             <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                          <local:SilverCrmLookup LookupStyle="Single" EntityName="account" Items="{Binding Converter={StaticResource lkConverter}, ConverterParameter=ParentCustomerId}" />
             </StackPanel>
</DataTemplate>

Converter Class:
public class LookupConverters : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                if (value != null)
                {
                    LookupItem item = new LookupItem() { Name = ((CRM2011GridView.ODATA.Contact)(value)).ParentCustomerId.Name, Id = (Guid)((CRM2011GridView.ODATA.Contact)(value)).ParentCustomerId.Id };
                    return new List<LookupItem>() { item };
                }
                else
                {
                    return null;
                }
            }
            catch
            { 
                
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

P.S *** Sorry, my English isn't good.  

Thanks

Nov 16, 2012 at 1:49 PM

Hi

I suggest you to try something like this.

<DataTemplate x:Key="LookupCell">
    <my:SilverCrmLookup Items="{Binding Lookup,Mode=TwoWay}" ObjectTypeCode="{Binding ObjectTypeCode}" LookupControlOnUserChangeValue="LookupControl_LookupControlOnUserChangeValue" EntityName="account" AutoComplete="True" PrimaryAttribute="name" Background="Transparent" FocusBackground="Transparent" BorderThickness="0" />
</DataTemplate>

create a class and bind a list of objects to the datagrid

public class MyClass : IEditableObject, INotifyPropertyChanged
    {
        List<LookupItem> _lookup;
        int _objectTypeCode;
        private event PropertyChangedEventHandler _propertyChanged;

        public int ObjectTypeCode { get { return _objectTypeCode; } set { _objectTypeCode = value; } }
        public List<LookupItem> Lookup
        {
            get { return _lookup; }
            set
            {
                _lookup = value;
                OnPropertyChanged("Lookup");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged { add { _propertyChanged += value; } remove { _propertyChanged -= value; } }

        #region IEditableObject

        private MyClass backupCopy;
        private bool inEdit;

        public void BeginEdit()
        {
            if (inEdit) return;
            inEdit = true;
            backupCopy = this.MemberwiseClone() as MyClass;
        }

        public void CancelEdit()
        {
            if (!inEdit) return;
            inEdit = false;
            this.Lookup = backupCopy.Lookup;
        }

        public void EndEdit()
        {
            if (!inEdit) return;
            inEdit = false;
            backupCopy = null;
        }

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (_propertyChanged != null)
            {
                _propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class MyClasses : ObservableCollection<MyClass>
    {
        public MyClasses()
        {
        }
    }

create list of MyClass objects

MyClasses classes = new MyClasses();

MyClass cls = new MyClass {Lookup=new List<LookupItem>(new LookupItem() { Name = ((CRM2011GridView.ODATA.Contact)(value)).ParentCustomerId.Name, Id = (Guid)((CRM2011GridView.ODATA.Contact)(value)).ParentCustomerId.Id };),
ObjectTypeCode=
1};
classes.Add(cls);
dataGrid1.ItemsSource = classes;


Hope this Helps.
Nov 20, 2012 at 6:42 AM

Hi ishancns,

 

Thanks for you reply. :)

I'll test it out and get back to you. 

 

Regards

 

Nov 26, 2012 at 10:11 AM

Hi ishancns,

 

I already changed the code as per you suggested. But no luck. I am still encountering same issue.

Lookup values are keep changing if I am scrolling down and up.

        void _contacts_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {

            MyClasses classes = new MyClasses();
            for(int i=0; i <_contacts.Count; i++)
            {               
                LookupItem templki = new LookupItem()
                {
                    Name = ((CRM2011GridView.ODATA.Contact)(_contacts[i])).ParentCustomerId.Name,
                    Id = (Guid)((CRM2011GridView.ODATA.Contact)(_contacts[i])).ParentCustomerId.Id,
                    Url = string.Empty
                };

                List<LookupItem> tmplk = new List<LookupItem>();
                tmplk.Add(templki);

                MyClass cls = new MyClass
                {
                    Lookup = tmplk,
                    ObjectTypeCode = 1
                };

                classes.Add(cls);
            }
            dataGrid1.ItemsSource = classes;

       }

 

<DataTemplate x:Key="LookupCell">
    <local:SilverCrmLookup Height="20" HorizontalAlignment="Left" Margin="150,160,0,0" Items="{Binding Lookup}" VerticalAlignment="Top" Width="203" EntityName="account" Icon="/MySilverlightApp1;component/Images/ico_16_2.png" LookupStyle="Single" />
</DataTemplate>