C1TrueDBGrid is known to be the best when it comes to its database interaction capabilities. But it has occasionally lacked to meet the increasing demand for UI customization. Even though C1TrueDBGrid provides methods and events to customize the appearance (like cell Foreground, Background), at times you may find yourself stuck on a specific requirement. This blog looks forward to solve one of these scenarios. Consider the requirement where you need to set the cell background for all the modified cells. Now some of you may raise a question here. C1TrueDBGrid provides the method ‘AddCellStyle’ method with one of its parameters accepting enumeration of ‘UpdatedCell’. This changes the backcolor of the edited cell. Perfect!!!. So what additional requirement we can look forward too. If you check the above implementation using ‘AddCellStyle’, you would find that once you shift the focus from currently edited row to another row, the background is gone. So how do we manage to maintain the background? To implement this, simply maintain a collection of all the edited cells and then using ‘OwnerDraw’ event, you can set the desired cell style. You can use ‘Point’ class to refer to a cell. In this blog we will consider ‘X’ property referring to Row index for a cell and similarly ‘Y’ property will refer to the Column index. Let’s see the required code implementation step by step below. 1. Declare a public ‘List’ object holding collection of ‘Point’ objects.
List<Point> modifiedList = new List<Point>();
2. Enable ‘OwnerDraw’ property for each Column.
foreach (C1.Win.C1TrueDBGrid.C1DisplayColumn cd in c1TrueDBGrid1.Splits[0].DisplayColumns)
cd.OwnerDraw = true;
3. In ‘AfterColEdit’ event, add the edited cell to the collection of Edited Cells.
void c1TrueDBGrid1_AfterColEdit(object sender, C1.Win.C1TrueDBGrid.ColEventArgs e)
{
int row = c1TrueDBGrid1.Row;
int col = c1TrueDBGrid1.Splits[0].DisplayColumns.IndexOf(e.Column);
modifiedList.Add(new Point(row, col));
}
4. In ‘OwnerDraw’ event, set the required style settings on the edited cell.
void c1TrueDBGrid1_OwnerDrawCell(object sender, C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs e)
{
Point mc = new Point(e.Row, e.Col);
if (modifiedList.Contains(mc))
e.Style.BackColor = Color.Red;
}
Final appearance after editing few of the cells looks like this. Download Sample