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

Change style of cell when edited

Hi, I''d like to change the Backcolor of a cell as soon as the cursor is in it and a key has been pressed. What event would i use ?

11 Replies

AD Administrator Syncfusion Team April 6, 2005 03:35 PM UTC

I would try using these events. grid.CurrentCellChanging += new CancelEventHandler(grid_CurrentCellChanging); grid.CurrentCellEditingComplete += new EventHandler(grid_CurrentCellEditingComplete); grid.PrepareViewStyleInfo += new GridPrepareViewStyleInfoEventHandler(grid_PrepareViewStyleInfo);
private bool cellChanged = false;

private void grid_CurrentCellChanging(object sender, CancelEventArgs e)
	this.cellChanged = true;
	this.grid.RefreshRange(grid.CurrentCell.RangeInfo, true);
private void grid_CurrentCellEditingComplete(object sender, EventArgs e)
	this.cellChanged = false;
private void grid_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
	if(this.cellChanged && grid.CurrentCell.HasCurrentCellAt(e.RowIndex, e.ColIndex))
		e.Style.BackColor = Color.Red;

AD Administrator Syncfusion Team April 6, 2005 03:53 PM UTC

What would i have to do to keep the changed color for the edited cell even if i select a different cell ?

AD Administrator Syncfusion Team April 6, 2005 03:58 PM UTC

What kind of grid are you using?

AD Administrator Syncfusion Team April 6, 2005 04:11 PM UTC


AD Administrator Syncfusion Team April 6, 2005 04:28 PM UTC

You will have to cache the records/fields that you want to have a special color. Then in PrepareViewStyleInfo, you would check if the style being prepared belongs to one of your cached record/fields, and if so, you would set the backcolor. In TableControlCurrentCellChanged, you would add the record/field to your cache. Then when you wanted to uncolor the cells, you could clear the cache or just remove certain record/fields from your cache. As far as a cache goes, you could use a hashtable where the key is some value that uniquely picks out the record/field. In version 3.2, we ship a sample that does something like this, though it actually stores style objects (not just back color) and uses QueryCellStyleInfo to apply these cached styles intead of just setting the BackColor in PrepareViewStyleInfo. Here is a link to this 3.2 sample. \Syncfusion\Essential Studio\\Windows\Grid.Windows\Samples\Grouping\EmployeeViewXmlIO\CS Version 3.2 is now available for download from:

AD Administrator Syncfusion Team April 7, 2005 12:58 PM UTC

Ok, got it almost working. But there still is a problem when I edit a child cell. As key to identify edited cells i''m using Colindex and Rowindex. Now i presume that these indexes are changed when a nested table is displayed. If i edit a cell in the first row of a childtable the same cell is marked in every other childtable i expand. Is there another way to uniquely identify a cell ? I thought of adding the cell itself as key to a hashtable but how do i query for that cell in TableControlPrepareViewStyleInfo ?? It should be way that works, even if different childtables are displayed of course. Other possibility would be a way to always lookup a cell using Col and Rowindexes or translate these indexes to a tablewide standard index not depending of expanded or collapsed children. Thanks

AD Administrator Syncfusion Team April 7, 2005 01:23 PM UTC

I think you will be better off using the GridRecord itself as the hashtable key. This will always be the same no matter what rowindex the record is displayed. The actual rowindex will vary everytime you expand/collapse a table or group, or when you sort things. It is not tied to a specific record. If you try to use rowindexes as keys, then you would have to adjust them with every expand/collapse/sort. So, try using the GridRecord as the hashtable key. You can then use as the hashtable value, an arraylist of changed column names. Again, you may not want to use something like teh colindex as again these are not necessarily tied to any particular field in the record.

AD Administrator Syncfusion Team April 7, 2005 01:49 PM UTC

Sounds good but: How do I get the GridRecord to a currentcell in TableControlCurrentCellChanging event? How do I get the GridRecord to the e.Inner in TableControlPrepareViewStyleInfo ? What is the difference between a GridRecord and a Record ?

AD Administrator Syncfusion Team April 7, 2005 02:18 PM UTC

Ok, i figured it out myself. There is no difference between a gridrecord and record. Or it can be implicitly casted. But now it works. Thanks again for the excellent support

AD Administrator Syncfusion Team April 7, 2005 03:03 PM UTC

Here is a sample doing what I described. http://www.syncfusion.com/Support/user/uploads/GGC_BookMark_19ddd87.zip Record is the baseclass for GridRecord. Record is used within the Syncfusion.Grouping assembly normally as it has no knownledge of a grid. GridRecord is normally used from Syncfusion.Windows.Forms.Grid.Grouping as it does have addition knowledge of the grid.

NI Niv September 28, 2005 06:02 PM UTC

Hi, I found this example very useful. I''m trying to implement the same in a GridDataBoundGrid now. However, I haven''t been able to find the equivalent of the GridRecord object in the GridDataBoundGrid. Right now, I have implemented it by using the ''row number'' as key in the hashtable (instead of the record object). Obviously, the display goes for a toss when the grid allows sorting. Is there an equivalent for GridRecord in a GridDataBoundGrid? Niv

Live Chat Icon For mobile
Up arrow icon