How to highlight the Row header and Column Header of Current Cell in WinForms DataGrid ?
By default in WinForms DataGrid (SfDataGrid), the column and row header of the current cell will not be highlighted. You can highlight the column and row header by creating custom renderer for the column header and row header derived from GridHeaderCellRenderer and GridRowHeaderCellRenderer.
public partial class Form1 : Form
{
/// <summary>
/// Initializes the new instance for the Form.
/// </summary>
public Form1()
{
InitializeComponent();
sfDataGrid1.DataSource = new OrderInfoCollection().OrdersListDetails;
sfDataGrid1.CellRenderers["Header"] = new CustomHeaderCellRenderer(this.sfDataGrid1);
sfDataGrid1.CellRenderers["RowHeader"] = new CustomRowHeaderCellRenderer(this.sfDataGrid1);
sfDataGrid1.CurrentCellActivated += SfDataGrid1_CurrentCellActivated;
}
private void SfDataGrid1_CurrentCellActivated(object sender, Syncfusion.WinForms.DataGrid.Events.CurrentCellActivatedEventArgs e)
{
sfDataGrid1.TableControl.Invalidate(sfDataGrid1.TableControl.GetRowRectangle(sfDataGrid1.TableControl.GetHeaderIndex(), false));
}
}
public class CustomHeaderCellRenderer : GridHeaderCellRenderer
{
SfDataGrid DataGrid { get; set; }
public CustomHeaderCellRenderer(SfDataGrid DataGrid)
{
this.DataGrid = DataGrid;
}
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
{
if (rowColumnIndex.ColumnIndex == DataGrid.CurrentCell.ColumnIndex)
{
style.BackColor = ColorTranslator.FromHtml("#0078d2");
style.TextColor = Color.White;
}
else
{
style.BackColor = DataGrid.Style.HeaderStyle.BackColor;
style.TextColor = DataGrid.Style.HeaderStyle.TextColor;
}
base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex);
}
}
public class CustomRowHeaderCellRenderer : GridRowHeaderCellRenderer
{
SfDataGrid DataGrid { get; set; }
public CustomRowHeaderCellRenderer(SfDataGrid DataGrid)
{
this.DataGrid = DataGrid;
}
protected override void OnRender(Graphics paint, Rectangle cellRect, string cellValue, CellStyleInfo style, DataColumnBase column, RowColumnIndex rowColumnIndex)
{
if (rowColumnIndex.RowIndex == DataGrid.CurrentCell.RowIndex)
style.BackColor = ColorTranslator.FromHtml("#0078d2");
else
style.BackColor = DataGrid.Style.RowHeaderStyle.BackColor;
base.OnRender(paint, cellRect, cellValue, style, column, rowColumnIndex);
}
}
Conclusion
I hope you enjoyed learning about how to highlight the row header and column header of current cell in DataGrid.
You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations WinForms DataGrid documentation and how to quickly get started for configuration specifications. You can also explore our WinForms DataGrid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!