We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

[GDBG]How to Change Cell BackColor when CurrentCellChanged

When I changed the text of a cell on UI,
I would like to be to change the cell's BackColor,
How can I really be done?

Thanks a lot.

4 Replies

RC Rajadurai C Syncfusion Team March 18, 2010 01:27 PM UTC

Hi Chen,

Thanks for your interest in Syncfusion Products.

In GridDataBoundGrid, the only thing stored on a cell by cell basis is the value (that is mapped to the DataSource, so grid does not actually hold this information either). Hence, you cannot use indexers on the GridDataBoundGrid to set any property other than CellValue (or Text as they are the same thing) as shown below.

this.gridDataBoundGrid1[0,2].BackColor = Color.BurlyWood;

To change a cell specific property, you should handle the PrepareViewStyleInfo event, and there check e.ColIndex and e.RowIndex, if they point to the respective cells and apply styles accordingly.

void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e)
{
if (e.RowIndex == 3 && e.ColIndex == 3)
e.Style.BackColor = Color.BurlyWood;
}

To set backcolor based on change happened in cell, you need to get the rowindex and columnindex of the currentcell from the CurrentCellChanged event and check it in the PrepareViewStyleInfo event, if it is the queried cell and set backcolor accordingly. Here is the code that can be handled.

if (e.RowIndex == rowIndex && e.ColIndex == colIndex)
e.Style.BackColor = Color.Red;
else
{
e.Style.BackColor = SystemColors.Window;
rowIndex = -1;
colIndex = -1;
}

Here rowIndex, colIndex values are retrieved through CurrentCellChanged event.

Regards,
Rajadurai


CC Chen Chi Chang March 23, 2010 03:29 AM UTC

Hi Rajadurai,
Thanks for your reply. Now I can change cell's BackColor by your method.
But it is not a permanent change.
I mean GDBG[1,3](already have changed its color) will change back to SystemColors when GDBG[1,4] change to red.
And when I highlighted this row, the color-changed cell turned back to SystemColors.

How could we change cell's color 'permanently'?

Thanks again.
Best Regards, Chen.


CC Chen Chi Chang March 23, 2010 03:49 AM UTC

Hi Rajadurai,
I already find out where the question is. I must store Row Index and Col Index of all the color-changed cell with a list.
Then always check the list and change the BackColor in PrepareViewStyleInfo.

Thanks for your reply again.
Best Regards, Chen.


RC Rajadurai C Syncfusion Team March 26, 2010 04:03 AM UTC

Hi Chen,

Thanks for your update.

Yes. In such cases, it is needed to retain the cell indexes in a collection and use the same in PrepareViewStyleInfo event to retain the backcolor. Glad to hear that you had got it working.

Thank you for using Syncfusion Products.

Regards,
Rajadurai

Loader.
Live Chat Icon For mobile
Up arrow icon