Hey,
I have a XamDataGrid, that displays a SQL-Table via a DataTable. Multiple users work on this table, so each of them can insert new rows.
Because of that I need to update my DataTable after each RecordUpdate.The problem is, that after the DataTable is refilled, the selection from before isn't selected anymore.
I cant select a row by the index, cause it can happen another user added a row, so the index of the row is not the same anymore.
IS there a way to select a row by the Identity in the ID-Column? Or any other way to select a row beside of the index.
Hey Andrew,
Your suggestion with the GetRecordFromDataItem method worked out.
My fault with the SelectedDataItem property was, that I obviously need to find the new DataRowView if Data has changed, and can't use the old DataRowView. So this would work too now.Greetings Benedikt
Hello Benedikt,
Thank you for your update on this matter.
If you have a very large data source, perhaps using the GetRecordFromDataItem method of the XamDataGrid and passing your data item that you wish to select would help you here. This should be faster than looping through each of your records in the XamDataGrid.Records collection.
Regarding setting the SelectedDataItem property, I would expect that this would work. Out of curiosity, were you setting this to the actual DataRow or the DataRowView that is in the DefaultView of your DataTable's view? The actual DataRow is not the data item in this case - it is the DataRowView in the table's view.
Please let me know if you have any other questions or concerns on this matter.
Hello Andrew,Your thoughts are all right.I tried your idea at first too.The problem is: Because of the amount of data, I cant loop through the whole XamDataGrid.Records. It takes way too long time.
I also tried to directly set the SelectedDataItem but that doesnt have any effect.
I have been investigating into your requirement in this case, and you have mentioned the DataTable being refilled, which I assume means that you are adding all new DataRow instances. If so, the behavior you are seeing is expected, as the XamDataGrid will generate new DataRecord elements for the new underlying data items in your table.
Assuming my impression of the above is correct, the best thing I can recommend in this case is to have a unique identifier or data column value for your DataRows that you can use. Each DataRecord element in the XamDataGrid.Records collection has a DataItem property that will return the underlying data item that the record represents. Assuming that this unique identifier would not change, you could get the underlying data items for your selected records, get the data items associated with them, and use the unique identifier to reselect the representative records after the change.
The code that you could use for this would involve utilization of the XamDataGrid.SelectedItems.Records collection, as this will contain all of the selected DataRecords in your grid. You could get a collection of the DataItem elements for each of those DataRecords with respect to the unique identifier, and then after making the change to your DataTable, loop through the XamDataGrid.Records collection searching for a DataItem that matches that unique identifier and set the DataRecord.IsSelected property to "true" for those items.
I hope this helps. Please let me know if you have any other questions or concerns on this matter.