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

question about 2.0.5.1\Grid\Samples\DataBound\GridPerf

Hi Clay or anyone who may know the answer, In this sample, if I use virtualgridcontrol, DataTable as source, what would be the best way to color the single cell? The senarios would be like if in column c, any number larger than 30 will be in red, or if any number in column larger than 30, the column A cell at the same row will be in red. Of course, the data will come, go and update in real time. I could record the current location in the gridcontrol after calling update, then color it by location, but there must be a smarter way to do it. Any suggestions? Thanks a lot Chris

1 Reply

AD Administrator Syncfusion Team August 11, 2004 06:54 AM UTC

You can use QueryCellInfo to set conditionally set the backcolors of cells. Here is code that will color columns 1 and 3 red if teh value in column 3 is larger than 30.
private void grid_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
	if(e.ColIndex > 0 && e.RowIndex > 0)
	{
		e.Style.CellValue = this.table.Rows[e.RowIndex-1][e.ColIndex-1];
		e.Handled = true;
		if(e.ColIndex == 3)
		{
			double d;
			if(double.TryParse(e.Style.Text, System.Globalization.NumberStyles.Any,
				null, out d) && d > 30)
			{
				e.Style.BackColor = Color.Red;
			}
		}
		else if(e.ColIndex == 1)
		{
			double d;
			if(double.TryParse(this.grid[e.RowIndex, 3].Text, 
				System.Globalization.NumberStyles.Any, 	null, out d) && d > 30)
			{
				e.Style.BackColor = Color.Red;
			}
		}
	}
}
One other thing, when you change teh value in column 3, you would want to make sure column 1 wa redrawn so it would reflect an y new color determined by the new value in column 3. You can do this in SaveCellInfo.
private void grid_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e)
{
	if(e.ColIndex > 0 && e.RowIndex > 0)
	{
		this.table.Rows[e.RowIndex-1][e.ColIndex-1] = e.Style.CellValue;
		e.Handled = true;
		if(e.ColIndex == 3)
			this.grid.Invalidate(grid.RangeInfoToRectangle(GridRangeInfo.Cell(e.RowIndex, 1)));
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon