We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Changing cell background color

Hi, is is possible to set a cell background color without using QueryCellStyle

Something like SetCellBackgroundColor(column,row)

Thanks

1 Reply

DY Deivaselvan Y Syncfusion Team November 19, 2018 09:16 AM UTC

Hi Chris, 
 
Thanks for contacting Syncfusion support. 
 
SfDataGrid is designed based on column-oriented architecture and it is not a cell-based grid. It is impossible to change the back color of any specific cell in SfDataGrid without using QueryCellStyle event.  
 
However, you can achieve your requirement to set background color of a cell using method like SetCellBackgroundColor. It can be achieved by maintaining a Dictionary with RowcolumnIndex of the cell as Key and the background color as value. Based on the value in the dictionary set the background color of the cell within the QueryCellStyle event. Please refer to the following code example and sample from the given location to change the background color of a specific cell in a button click event. 
 
Code example :  
 
Dictionary<RowColumnIndex, Color> colorDict = new Dictionary<RowColumnIndex, Color>(); 
 
void SetCellBackgroundColor(RowColumnIndex rowColumnIndex, Color color) 
{ 
    if (!colorDict.ContainsKey(rowColumnIndex)) 
        colorDict.Add(rowColumnIndex, color); 
    else 
        colorDict[rowColumnIndex] = color; 
 
    sfDataGrid.TableControl.Invalidate(this.sfDataGrid.TableControl.GetCellRectangle(rowColumnIndex.RowIndex, rowColumnIndex.ColumnIndex, false)); 
} 
 
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.LightBlue); 
} 
 
 
 
Regards, 
Deivaselvan 


Loader.
Live Chat Icon For mobile
Up arrow icon