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

conditional formatting

Hi, I would like to display different color when the "newValue > oldValue" and "newValue < oldValue" for a particular period and then goes off. I cannot find any ways I can obtain the previous cell value, therefore I cannot do the above comparison. May you please give me some hints on it? Thanks! Eric

3 Replies

AD Administrator Syncfusion Team April 19, 2005 11:01 AM UTC

You did not indicate what grid you were using or what event you are in. But, in general, none of our grids maintain a cache of changed values. If you need this, then you would have to maintain it yourself. If you are in a non-virtual GridControl, you could handle the CurrentCellvalidating event. There you can get the new value from grid.CurrentCell.Renderer.ControlText, and get the old value from grid[cc.RowIndex, cc.ColIndex].CellValue. Then you could explicitly set grid[cc.RowIndex, cc.ColIndex].BackColor. This way you would not have to cache anything. But if you are using a virtual GridControl or a GridDataBoundGrid, you woul dhave to maintain some kind of cache of cells that you want colored. For example, in the CurrentCellValidating event handler described above, you would have to cache the cell and the color. You could create a key value based on the row and column index, and use this key value as a hashtable key to save the color. Then in a PrepareViewStyleInfo event handler, you could check if the e.RowIndex and e.ColIndex forms a key in your hashtable. If they do, then retrieve the color and set it to e.Style.BackColor.


EL Eric Luk April 22, 2005 10:52 AM UTC

Thanks a lot! In fact, we are using the virtual grid with grid control. We still haven''t sorted out the coloring and color-turn-off mechanism yet. As our grid performance is critical, we don''t want to implement the PrepareViewStyleInfo events (as it got called very frequent, plus it cannot serve the "timer"+"color turn off" function.) Do you have any suggestion to implement it without big impact on the performance? (i.e. Change the cell color by comparing the before and after values, and then reset the color upon 3 sec time if no updates acted on the cell). Thanks for your advice. We appreciate your help!


AD Administrator Syncfusion Team April 22, 2005 11:46 AM UTC

In a virtual grid, you normally do things dynamically in QueryCellInfo. If you want to color cells at this point based on changes your user makes to cell values, then I think you will have to cache the changed cells and their associated color, and then use this cache in QueryCellInfo to set the e.Style.BackColor when e.RowIndex and e.COlIndex point to one of your changed cells. For example, in CurrentCellChanging, you could test if you want to have a color assigned to the current cell that was changed. If so, create a key value using the CurrentCell.RowIndex and CurrentCell.ColIndex. Then add this key and the Color to a hashtable. Then in QueryCellInfo, use e.RowIndex and E.ColIndex to generate a key value, test if this keyvalue is on teh hashtable, and if so set e.Style.BackCOlor to the cached color. Here are the changes I made to our VirtualGridTutorial sample to illustrate this idea.
private Hashtable savedColors = new Hashtable();
private Color lessColor = Color.Red;
private Color moreColor = Color.Green;

private void gridControl1_CurrentCellValidating(object sender, CancelEventArgs e)
{
	GridCurrentCell cc = this.gridControl1.CurrentCell;
	int oldValue = (int)this.gridControl1[cc.RowIndex, cc.ColIndex].CellValue;
	try
	{
		int newValue = int.Parse(cc.Renderer.ControlText);
		if(newValue < oldValue)
		{
			int key = GetKey(cc.RowIndex, cc.ColIndex);
			if (savedColors.ContainsKey(key))
				savedColors[key] = lessColor;
			else
				savedColors.Add(key, lessColor);
		}
		else if(newValue > oldValue)
		{
			int key = GetKey(cc.RowIndex, cc.ColIndex);
			if (savedColors.ContainsKey(key))
				savedColors[key] = moreColor;
			else
				savedColors.Add(key, moreColor);
		}
	}
	catch{}
}

private int GetKey(int row, int col)
{
	return 100000 * col + row;
}

void GridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e) 
{
	if (e.RowIndex > 0 && e.ColIndex > 0)
	{
		e.Style.CellValue = this._extData[e.RowIndex - 1, e.ColIndex - 1];

		int key = GetKey(e.RowIndex, e.ColIndex);
		if(savedColors.ContainsKey(key))
			e.Style.BackColor = (Color) savedColors[key];
		e.Handled = true;
	}
}

Loader.
Live Chat Icon For mobile
Up arrow icon