This is a migrated thread and some comments may be shown as answers.

GridView.Items.IndexOf Result -1

1 Answer 98 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
n/a
Top achievements
Rank 1
n/a asked on 15 Feb 2010, 07:08 PM
I am trying to create a status column that shows yellow when a record update is being pushed to the server, and changes to green when the update is complete, but am having trouble.  The following is my code...

public void dg_RowLoaded(object sender, RowLoadedEventArgs e) { 
            e.Row.Cells[0].Content = (dg.Items.IndexOf(e.Row.Item) + 1).ToString(); 
        } 
        public void dg_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e) { 
            if (e.EditAction == GridViewEditAction.Commit) { 
                e.Row.Cells[0].Background = new SolidColorBrush(Colors.Yellow); 
                _ws.SetContactCompleted += new EventHandler<ServiceReference1.SetContactCompletedEventArgs>(_ws_SetContactCompleted); 
                _ws.SetContactAsync((ServiceReference1.Contact)e.Row.Item); 
            } else { 
                dg.CancelEdit(); 
            } 
        } 
        public void _ws_SetContactCompleted(object sender, ServiceReference1.SetContactCompletedEventArgs e) { 
            int iIndex = dg.Items.IndexOf(e.Result); 
            DependencyObject iDO = dg.ItemContainerGenerator.ContainerFromIndex(iIndex); 
        } 

In dg_RowEditEnded I pass a Contact to the webservice to be updated, and after update it returns the same Contact back in e.Result in the _ws_SetContactCompleted method.  However, when I call IndexOf it returns -1.  I have checked the passed and return values from the webservice, and it is the exact same object.

Any help is much appreciated.

Thanks

1 Answer, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 16 Feb 2010, 03:09 PM
Hi Ari Perlstein,

I have checked the code you have sent to me and prepared sample project that demonstrates how to achieve the desired behavior.

There are two major reasons that prevent your code to work correctly:

1.Using this code:
_ws.SetContactAsync((ServiceReference1.Contact)e.Row.Item)
  You had passed the old state of the object. However the state of the object has been already updated with the new value. (e.g. Editing Name from Charlie to John. Service will receive Charlie while Silverlight will keep John). This will result in difference between object in e.Result and the one kept in gridview itemsSource.

2. When you receive entity from the WCF it represent different object (has different hashcode) than the one in the silverlight application. Thus you need to provide mechanism (override equals of get hashcode) to compare these objects.

Let me know if this works for you.

Greetings,
Tsvyatko
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
n/a
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
Share this question
or