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

GridRangeInfo

Hello,

I would like to change the background color of a cell whenever its numeric value becomes negative. To do this, I thought I'd use the GridControl.GridCellsChanged event and then iterate through the cells that have changed to determine whether they should have their background color updated. However, I can't figure how to perform this iteration using the GridRangeInfo object. Does anyone have a some sample code illustrating this or possibly even a better approach.

Thanks,
AP


3 Replies

AD Administrator Syncfusion Team May 16, 2008 05:37 AM UTC

Hi AP,

Thanks for the interest in Syncfusion Grid.

You can handle QueryCellStyleInfo event and can change the backcolor of the cell if the value is a negative. You can check the CellValue of the CurrentCell and can set the backcolor if the CellValue is less than 0. Please refer the following code snippet that shows how we can handle QueryCellStyleInfo event to get the this behavior.


void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
if (e.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell
|| e.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell)
{
if (e.TableCellIdentity.Column.MappingName == "B")
{
int cellvalue = Convert.ToInt32(e.Style.CellValue);
if (cellvalue < 0)
{
e.Style.BackColor = Color.Red;
//e.Style.Format = "{#,##}";// just a format
}
}
}
}


Please refer the sample in the below link that illustrates the above.

http://websamples.syncfusion.com/samples/Grid.Windows/F73730/main.htm

Please let me know if you have any questions.

Regards,
Asem.




AD Administrator Syncfusion Team May 16, 2008 01:24 PM UTC

Hi Asem,

Thanks for your response. The problem is that I'm not using the GridGroupingControl. I'm using the regular GridControl object. There is no QueryCellStyleInfo event for that class.

>Hi AP,

Thanks for the interest in Syncfusion Grid.

You can handle QueryCellStyleInfo event and can change the backcolor of the cell if the value is a negative. You can check the CellValue of the CurrentCell and can set the backcolor if the CellValue is less than 0. Please refer the following code snippet that shows how we can handle QueryCellStyleInfo event to get the this behavior.


void gridGroupingControl1_QueryCellStyleInfo(object sender, GridTableCellStyleInfoEventArgs e)
{
if (e.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell
|| e.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell)
{
if (e.TableCellIdentity.Column.MappingName == "B")
{
int cellvalue = Convert.ToInt32(e.Style.CellValue);
if (cellvalue < 0)
{
e.Style.BackColor = Color.Red;
//e.Style.Format = "{#,##}";// just a format
}
}
}
}


Please refer the sample in the below link that illustrates the above.

http://websamples.syncfusion.com/samples/Grid.Windows/F73730/main.htm

Please let me know if you have any questions.

Regards,
Asem.






AD Administrator Syncfusion Team May 17, 2008 10:44 AM UTC

Hi AP,

You can handle QueryCellInfo for the GridControl. You can also use CurrentCellValidated event and can check if the value is

negative and change the backcolor of the cell. Please refer the following code snippet for more detail.


private void gridControl1_CurrentCellValidated(object sender, EventArgs e)
{
GridCurrentCell cc = this.gridControl1.CurrentCell;
string s = cc.Renderer.ControlText;
int i = Convert.ToInt32(s);
if (i < 0)
{
this.gridControl1[cc.RowIndex, cc.ColIndex].BackColor = Color.Green;

}
}


Please refer sample in the below link that illustrates the above.

http://websamples.syncfusion.com/samples/Grid.Windows/73730B/main.htm

Please let me know if this not what you needed.

Regards,
Asem.



Loader.
Live Chat Icon For mobile
Up arrow icon