BoldDeskWe are launching BoldDesk on Product Hunt soon. Learn more & follow us.
Hello,
It is possidble to change a gridrow backcolor after the grid is rendered? (so not trough grid events etc..)
I have two grid (primary, secondary) and I need change the backcolor of specified rows in the secondary table based on selection in the primary table. Every row selection in the primary table causes backcolor change in the secondary table in specified rows.
That is possible?
Thank you very much for help!
Best Regards, SZL
Hi SZL,
Your requirement to change the back color of specified rows in the secondary
table based on selection in the primary table can be achieved by customizing
the SelectionChanged event and set the back color for selected items in
secondary table. Refer to the below code snippet,
// Event subscription sfDataGrid.SelectionChanged
+= SfDataGrid_SelectionChanged; private void SfDataGrid_SelectionChanged(object sender, Syncfusion.WinForms.DataGrid.Events.SelectionChangedEventArgs e) { // Customize based on your scenario // Set the Selected item for second SfDataGrid sfDataGrid1.SelectedItem = sfDataGrid.SelectedItem; }
// Set the Selection color for Second Grid this.sfDataGrid1.Style.SelectionStyle.BackColor = Color.LightSeaGreen; |
UG Link: https://help.syncfusion.com/windowsforms/datagrid/selection#appearance
https://help.syncfusion.com/windowsforms/datagrid/selection#programmatic-selection
https://help.syncfusion.com/windowsforms/datagrid/selection#displaying-message-box-on-selection-changed
Find the sample demo in the attachment.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Thank you very much for the example!
A little problem with this, that I not want to modify the SelectedItems collection in second grid (the row backcolor change is only a highlight for the end-user).
SZL,
Your requirement can be achieved by customizing the SelectionChanged and
QueryRowStyle events. Refer to the below code snippet,
// Event subscription sfDataGrid.SelectionChanged +=OnSelectionChanged; // Event subscription sfDataGrid1.QueryRowStyle += OnQueryRowStyle;
//Event customization private void OnQueryRowStyle(object sender, QueryRowStyleEventArgs e) { // Customize based on your scenario if(sfDataGrid.SelectedItem != null) { // Customize based on your scenario if (isAddedSelected && e.RowData == sfDataGrid.SelectedItem) { // Set the backGround color for the row e.Style.BackColor = Color.LightBlue; isAddedSelected = false; } // if you want to remove the backcolor of row, set the white color for selected items else if (isRemovedSelected) { // Reset the backGround color for the row e.Style.BackColor = Color.White; isRemovedSelected = false; }
} }
// Maintain the flag for selection related backcolor applied in row bool isAddedSelected; bool isRemovedSelected;
//Event customization private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) {
// Customize based on your scenario if (e.AddedItems.Count > 0) { isAddedSelected = true; foreach (var item in e.AddedItems) { // Get the row index of the selected item var rowIndex = sfDataGrid1.TableControl.ResolveToRowIndex(item); // Invalidate the row to refresh the row sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(rowIndex, true)); }
}
// if you want to remove the backcolor of selection removed row, here refresh the row if (e.RemovedItems.Count > 0) { isRemovedSelected = true; foreach (var item in e.RemovedItems) { // Get the row index of the selection removed item var rowIndex = sfDataGrid1.TableControl.ResolveToRowIndex(item); // Invalidate the row to refresh the row sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(rowIndex, true)); }
} } |
UG Link: https://help.syncfusion.com/windowsforms/datagrid/conditionalstyling#styling-based-on-content-1
Find the sample demo in the attachment.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the solution so that
other members can locate it more quickly.
Thank you very much, it will be fine!
Best Regards,
László
SZL,
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.