Hi,
I create a context menu for xamdatagrid with two option 'Delete' and 'Restore'.
When user select the 'Delete' Option the appropriate row background color should change to 'Red'.
When user select the 'Restore' option the appropriate row background color should back to normal.
Now i need the code for 'Delete' and 'Restore'?
If this issue is solvable, then many question in this forum will be resolve.
I am waiting for experts reply, Thank - Ramesh.P
I have been going thru the fourm for a simple solution to set the background color of a row depending on a criteria.
most of the suggestions point to a xaml datatrigger like
Here are the issues I have:
RecordCollectionBase recordsCollection = xamDataGrid1.Records;foreach (Record rec in recordsCollection) { DataRecord dataRec = (DataRecord)rec; if ("Sales" == (string)dataRec.Cells["department"].Value) { DataRecordPresenter.FromRecord(rec).Background = Brushes.Orange; } }
can you please suggest a simple solution to the row background coloring with a sample code solution if possible. Thanks
Rich,
Try setting the RecordContainerGenerationMode to PreLoad. If you are using this approach, you have to change that, because the Default value is recycling and the DataRecordPresenters are reused and that is why you are seeing Background color to the incorrect record. The other way is as you have already found out, clearing the value. Please note that you can use .ClearValue(...) method instead of setting it to transparent.
Alex,
Thank you for the hint - works much better. My problem now, however, is that as I scroll down in the xamDataGrid I see rows w/red background that should not have it (don't meet the "if" below. Here is my code sample:
private void gridOrderPoolList_RecordsInViewChanged(object sender, RecordsInViewChangedEventArgs e){ XamDataGrid orderDataGrid = (XamDataGrid)e.Source; Record[] myRecords = orderDataGrid.GetRecordsInView(false); //no children records foreach (DataRecord dr in myRecords) { BizSessionViewEntity row = (BizSessionViewEntity)dr.DataItem; if (row.MRN == "075837714") //example of identifying a given row DataRecordPresenter.FromRecord(orderDataGrid.Records[dr.Index]).Background = new SolidColorBrush(Colors.Red); }}
What am I missing? I don't understand how the background of the unrelevant rows gets changed. My work-around is to have an "else" for the above "if" and change the color back to SolidColorBrush(Colors.Transparent), but I'd rather not do that. Any feedback would be greatly appreciated.
It only happens when I start scrolling in the grid.
-Rich
This is because, the XamDataGrid uses virtualization by default. Any DataRecordPresenters that are not currently visible are not created - they are virtualized. That is why you are getting an exception. You should create a style for these DataRecordPresenter or do this only for the records that are in view. You can get them with the GetRecordsInView(...) method of the XamDataGrid. You can keep this consistent by handling this in the RecordsInViewChanged event - this will fire each time a records gets into view.
Hope this helps.
I was trying to use the above in the following way: in a method where I specify DataSource for my grid (gridOrderPoolList) I have the following after the grid has data:
foreach
(DataRecord dr in gridOrderPoolList.Records)
{
BizSessionViewEntity row = (BizSessionViewEntity)dr.DataItem;
if (row.MRN == "046733083") //example of identifying a row by some value
DataRecordPresenter.FromRecord(gridOrderPoolList.Records[dr.Index]).Background = new SolidColorBrush(Colors.Red);
}
At run time I get the exception that "object reference not set to an instance of an object" and it fails on the line where I use DataRecordPresenter. Any ideas?
Thank you for your help.