I have a datagrid with datbound columns. Whenever a use edits changes a cell my grid, I want to show the chnage by changing the cells background & foreground.
I have tried the code below and nothing seems to work. Any ideas?
private void MainGrid_CurrentCellEditingComplete(object sender, System.EventArgs e)
GridStyleInfo style = MainGrid[MainGrid.CurrentCell.RowIndex, MainGrid.CurrentCell.ColIndex];
BrushInfo bi = new BrushInfo(GradientStyle.PathRectangle, Color.White, Color.Silver);
style.Interior = bi;
MainGrid.RefreshRange(GridRangeInfo.Cell(MainGrid.CurrentCell.RowIndex, MainGrid.CurrentCell.ColIndex), true);
ADAdministrator Syncfusion Team August 6, 2003 10:25 PM
In a GridDataBoundGrid, you cannot set individual cell properties (other than Text or CellValue). The reason is that the only 'cell storage' is your datasource, and it only knows the value. It does not track BackColor or any other properties.
So, to do this, you could maintain a list of changed objects, and then in PrepareViewStyleInfo, you could check to see if the cell being requested is in your list, and if so, color it there. Attached is a little sample. It uses a simple arraylist to track the changes with the primary key for the row and mappingname for the column forming the lookit item to see if a cell has been changed. Using such a lookup item allows you to sort the grid or move columns without having to constantly modify the changedcells list. If the ArrayList is too slow (you are doing hundreds/thousand of changes), then you could use a hashtable that might be quicker. Another comment is that to monitor the changed cells and add then to the list, the sample uses Model.SaveCellInfo. The reason is that this event will capture things like selecting a range of cells and pressing Delete to clear them, and CurrentCellEditingComplete is not hit in such cases.
ADAdministrator Syncfusion Team August 7, 2003 04:40 PM
Thanks. Would StyleModifyType work in SaveCellinfo?
ADAdministrator Syncfusion Team August 7, 2003 10:43 PM
I am not sure what you mean by 'StyleModifyType work in SaveCellInfo'. I am not sure there is a use for it there exacpt to possibly modify the e.Style.CellValue as that is the only thing that is used in a GridDataBoundGrid at this point.
In a GridDataBoundGrid, there is nothing stored for a cell except the CellValue. The backcolor is not stored (unless you do it directly yourself, allocating some datastructure to hold it, etc). So, in a GridDataBoundGrid, the purpose of SaveCellInfo, is you give you the chance to modify e.Style.CellValue before it goes into your datatsource, and also to let you know that this is about to happen. You can also use it to store things like e.Style.BackColor for a particular cell (e.RowIndex, e.ColIndex) if you want to do store these values yourself. But the GridDataBoundGrid will not put them anywhere.
ADAdministrator Syncfusion Team August 7, 2003 11:44 PM
I see. Thank you for the excellent explanation.
Please correct me if I am wrong but I think I may be looking for the flexibilility of your GridControl (which gives is more customizable - it remembers cell by cell styles, correct?) and also the databinding of your databound grid.
If the above is correct, would it start to be easier to modify the GridControl to be databound raher than me slow the databoundgrid for custinization? (i.e. GridControl has ChangeSTyles() - griddatabound does not)
Is their a way to make GridControldatabound - auto fill and update to a datasource with only a medium amount of customization?
ADAdministrator Syncfusion Team August 8, 2003 12:00 AM
If you use a GridControl in a non-virtual manner, then the grid does can store style information on a cell by cell basis.
You can call the GridControl.PopulateValues method to move a datatable into the internal storage of a grid. If you do so, then you can explicitly set style properties on individual cells. The draw back is that there is not 'automatic' way of moving any changes from your GridControl back to the DataTable. If your grid is for display purposes only, then this is not a problem. But, if you want your user to be able to edit cells, then it is.
One solution is to handle the SaveCellInfo event which is called any time a value is changed in the GridControl. You could then explicilty save the changed value back to the DataTable (in addition to letting the grid store it internally). One technical problem arises if you are allowing users to move rows and/or columns, but that can be handled as well through including a primary key column in your grid (maybe hiding from the user, but having it available to identify the proper record in the datatable).
If you use a GridDataBoundGrid, these getting the data back into the DataSource is managed for you, but to set cell-specific properties requires you to handle an event.
So, I think you would have to judge which path is less complicated in your specific use case.
ADAdministrator Syncfusion Team August 8, 2003 06:37 AM