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

backcolor of a cell in GridDataBoundGrid

I need to change the background color of a cell in a GridDataBoundGrid. I do it depending on the value of another cell in the grid. I found an example on this site that changes a cell backcolor depending on it''s own value, I tried to modify it a little to get it to change the backcolor of the cell and the preceding one in the same row. (I''m aware of the problem if I change the cells of the 1st column of the grid but that''s not important now) private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, Syncfusion.Windows.Forms.Grid.GridPrepareViewStyleInfoEventArgs e) { if(e.Style.Text == "v") { int col = e.ColIndex; int row = e.RowIndex; e.Style.BackColor = Color.Blue; /* this one works*/ gridDataBoundGrid1[row, col-1].BackColor = Color.Red; /* this does not work*/ } } does anyone have an idea why this isn''t working as I want??? (the examples on syncfusion''s site are on: http://www.syncfusion.com/support/EvaluationCenter/viewsource.aspx?lang=csharp&id=157 and the full project can be downloaded from: http://www.syncfusion.com/support/EvaluationCenter/default.aspx?cNode=5 ) thank you

1 Reply

AD Administrator Syncfusion Team March 16, 2005 02:47 PM UTC

In PrepareViewStyleInfo, when e.RowIndex, e.ColIndex point to teh cell you might want to color, then you should do your check at that point, and set e.Style.backColor if the check indicates it should be colored. //to make sure the whole row is refreshed as needed, in form.load, set this.grid.Model.Options.RefreshCurrentCellBehavior = GridRefreshCurrentCellBehavior.RefreshRow;
//the handler
private void gridDataBoundGrid1_PrepareViewStyleInfo(object sender, 
       GridPrepareViewStyleInfoEventArgs e)
	//color col 2 if col 1 is "v"
	if(e.ColIndex == 2 && e.RowIndex > 0)
		string s = this.gridDataBoundGrid1[e.RowIndex, 1].Text;
		if(s == "v")
			e.Style.BackColor = Color.Red;

Live Chat Icon For mobile
Up arrow icon