Giter Site home page Giter Site logo

syncfusionexamples / how-to-highlight-the-row-header-and-column-header-of-current-cell-in-winforms-datagrid Goto Github PK

View Code? Open in Web Editor NEW
0.0 4.0 1.0 39 KB

How to highlight the Row header and Column Header of Current Cell in WinForms DataGrid (SfDataGrid)?

C# 100.00%

how-to-highlight-the-row-header-and-column-header-of-current-cell-in-winforms-datagrid's Introduction

How to highlight the Row header and Column Header of Current Cell in WinForms DataGrid (SfDataGrid)?

About the sample

This sample illustrates how to highlight the Row header and Column Header of Current Cell in WinForms DataGrid.

By default in 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);
    }
}

Highlight Column and Row Header of current cell

Requirements to run the demo

Visual Studio 2015 and above versions

how-to-highlight-the-row-header-and-column-header-of-current-cell-in-winforms-datagrid's People

Contributors

farjanaparveen avatar mohanramanbukkarasu avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.