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.
Unfortunately, activation email could not send to your email. Please try again.

conditional formatting

Thread ID:

Created:

Updated:

Platform:

Replies:

27823 Apr 19,2005 06:23 AM Apr 22,2005 07:46 AM Windows Forms 3
loading
Tags: GridControl
Eric Luk
Asked On April 19, 2005 06:23 AM

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

Administrator [Syncfusion]
Replied On April 19, 2005 07:01 AM

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.

Eric Luk
Replied On April 22, 2005 06:52 AM

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!

Administrator [Syncfusion]
Replied On April 22, 2005 07:46 AM

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;
	}
}

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;