Change selected rows background color on a button press

Change selected rows background color on a button press

Hi 

I have a sfDatagrid and a number of rows selected, I then press a button and a value from a cell in each row is added to a listbox, this is working fine,

before i leave the button event i want to change the  background color of each selected row (to green) to show it has been added.

I am missing something silly i feel

Thanks 

Peter


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team February 24, 2021 06:01 PM UTC

Hi Peter,

Thank you for contacting Syncfusion support. 

Your requirement can be achieve by maintaining a Dictionary with RowColumnIndex of the cell as Key and the background color as value. Based on the value in the dictionary, QueryRowStyle event set the background color of the SelectedItems. Please refer the below code snippet for your reference, 
sfDataGrid1.QueryRowStyle += SfDataGrid1_QueryRowStyle; 
 
Dictionary<RowColumnIndex, Color> colorDict = new Dictionary<RowColumnIndex, Color>(); 
private void SfDataGrid1_QueryRowStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryRowStyleEventArgs e) 
{ 
            var rowColumnIndex = new RowColumnIndex(e.RowIndex,0); 
            if (colorDict.ContainsKey(rowColumnIndex)) 
                e.Style.BackColor = colorDict[rowColumnIndex]; 
} 
 
void SetCellBackgroundColor(RowColumnIndex rowColumnIndex, Color color) 
{ 
            if (!colorDict.ContainsKey(rowColumnIndex)) 
                colorDict.Add(rowColumnIndex, color); 
            else 
                colorDict[rowColumnIndex] = color;            
}        
 
private void sfButton2_Click(object sender, EventArgs e) 
{ 
            foreach (var selectedItem in sfDataGrid1.SelectedItems) 
            { 
                var index = sfDataGrid1.TableControl.ResolveToRowIndex(selectedItem); 
                SetCellBackgroundColor(new RowColumnIndex(index, 0), Color.Green); 
            } 
} 
Please let us know, if you require further assistance on this.

Regards,
Vijayarasan S 


Marked as answer

PE Peter February 24, 2021 09:02 PM UTC

Hi Vijayarasan S 

That is 90 percent there thank you very much

This remembers the row index regardless of data so if i filter the data in the "green" rows change.

however from here i can sort it by storing the rows unique identifier in the dictionary instead of the index and using it to apply the colors !!

thank you 

Peter


VS Vijayarasan Sivanandham Syncfusion Team February 25, 2021 02:24 PM UTC

Hi Peter, 
 
Thanks for the update. 
 
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😊. 
 
Regards, 
Vijayarasan S 


Loader.
Up arrow icon