Code Monkey home page Code Monkey logo

how-to-change-the-particular-cell-background-color-at-run-time-in-windows-forms-datagrid's Introduction

How to change the particular cell background color at run time in Windows Forms DataGrid(SfDataGrid)

About the sample

This example illustrates to change the particular cell background color at run time in Windows Forms DataGrid(SfDataGrid)

SfDataGrid provides the support to customize the background color of a single cell at run time. You can achieve this by maintaining a Dictionary with RowColumnIndex of the cell as Key and the background color as value. Based on the value in the dictionary, QueryCellStyle event set the background color of the cell.

 Dictionary<RowColumnIndex, Color> colorDict = new Dictionary<RowColumnIndex, Color>();

 this.sfDataGrid.QueryCellStyle += sfDataGrid_QueryCellStyle;

 void sfDataGrid_QueryCellStyle(object sender, Syncfusion.WinForms.DataGrid.Events.QueryCellStyleEventArgs e)
 {
     var rowColumnIndex = new RowColumnIndex(e.RowIndex,e.ColumnIndex);
     if (colorDict.ContainsKey(rowColumnIndex))
         e.Style.BackColor = colorDict[rowColumnIndex];
 }
 

 private void button1_Click(object sender, System.EventArgs e)
 {
     SetCellBackgroundColor(new RowColumnIndex(1, 0), Color.Red);
 }

 void SetCellBackgroundColor(RowColumnIndex rowColumnIndex, Color color)
 {
     if (!colorDict.ContainsKey(rowColumnIndex))
         colorDict.Add(rowColumnIndex, color);
     else
         colorDict[rowColumnIndex] = color;
     //Refresh the particular cell
     sfDataGrid.TableControl.Invalidate(this.sfDataGrid.TableControl.GetCellRectangle(rowColumnIndex.RowIndex, rowColumnIndex.ColumnIndex, false));
 }

Requirements to run the demo

Visual Studio 2015 and above versions

how-to-change-the-particular-cell-background-color-at-run-time-in-windows-forms-datagrid's People

Contributors

farjanaparveen avatar susmitha-sundar 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.