Every programmer has its own unique requirements. The other day one of our customers required Highlighting the entire Row while moving the cursor on C1TrueDbGrid. This blog explains an approach to Highlight the Row on which the cursor is pointing at the moment. We will implement this with the help of two events: MouseMove and OwnerDrawCell event. We will first fetch the Row index of the pointer where the cursor currently points in C1TrueDbGrid's MouseMove event. Then we will use the same index in OwnerDrawCell event to specify which element of the C1TrueDbGrid should be drawn by the grid and how it should be rendered. We will start with the binding of C1TrueDbGrid with some DataSource. Once it is done we need to enable the OwnerDraw property of each DisplayColumn in C1TrueDbGrid.
foreach (C1.Win.C1TrueDBGrid.C1DisplayColumn c in c1TrueDBGrid1.Splits[0].DisplayColumns)
c.OwnerDraw = true;
Now we will start with the actual implementation. In the MouseMove event we will fetch the Row index on which the cursor is hovered and save it in some global variable to be used across the OwnerDrawCell event.
void c1TrueDBGrid1_MouseMove(object sender, MouseEventArgs e)
{
if ((this.c1TrueDBGrid1.PointAt(e.X, e.Y) == C1.Win.C1TrueDBGrid.PointAtEnum.AtDataArea))
{
c1TrueDBGrid1.RefreshRow(rowIndex);
rowIndex = c1TrueDBGrid1.RowContaining(e.Y);
c1TrueDBGrid1.RefreshRow(rowIndex);
}
}
In the OwnerDrawCell event we will set the DrawCellFlags property to specify how custom renderer handles the background, border, content, etc.
void c1TrueDBGrid1_OwnerDrawCell(object sender, C1.Win.C1TrueDBGrid.OwnerDrawCellEventArgs e)
{
if (e.Row == rowIndex)
{
e.DrawCellFlags -= C1.Win.C1TrueDBGrid.Styles.DrawCellFlags.Background;
Rectangle rct = new Rectangle(e.CellRect.X, e.CellRect.Y, e.CellRect.Width, e.CellRect.Height);
e.Graphics.FillRectangle(new SolidBrush(Color.Plum), rct);
e.Graphics.DrawString(e.Text, c1TrueDBGrid1.Font, new SolidBrush(Color.Black), e.CellRect);
e.Handled = true;
}
else
{
e.DrawCellFlags = C1.Win.C1TrueDBGrid.Styles.DrawCellFlags.All;
}
}
Please refer to the attached sample for the detailed implementation of the above code. Download Sample_C# Download Sample_VB