Cange datagridview cel backcolor if cel has value
-
Hi, I use this to change the back color of a cell:
private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value.ToString()))
{
e.CellStyle.BackColor = Color.LightGoldenrodYellow;
}
}This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,
0200 A9 23 0202 8D 01 80 0205 00
-
Hi, I use this to change the back color of a cell:
private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value.ToString()))
{
e.CellStyle.BackColor = Color.LightGoldenrodYellow;
}
}This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,
0200 A9 23 0202 8D 01 80 0205 00
One way to distinguish columns is to check the ColumnIndex property of the DataGridViewCellFormattingEventArgs object. For example you can access the actual column being formatted using
dataGridViewData.Columns[e.ColumnIndex]
For more info, refer to DataGridViewCellFormattingEventArgs[^]
-
One way to distinguish columns is to check the ColumnIndex property of the DataGridViewCellFormattingEventArgs object. For example you can access the actual column being formatted using
dataGridViewData.Columns[e.ColumnIndex]
For more info, refer to DataGridViewCellFormattingEventArgs[^]
Hi, I've already been there, but I wanted something simple. Have it working now, this is what I use:
private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int x =e.ColumnIndex;
int y = e.RowIndex;if (dataGridViewZakdata\[x,y\].GetType() == typeof(DataGridViewTextBoxCell)) { if (!string.IsNullOrEmpty(e.Value.ToString())) { e.CellStyle.BackColor = Color.LightGoldenrodYellow; } } }
Thank You for your efford, Groover,
0200 A9 23 0202 8D 01 80 0205 00
-
Hi, I use this to change the back color of a cell:
private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value.ToString()))
{
e.CellStyle.BackColor = Color.LightGoldenrodYellow;
}
}This works, but also check box cells are changed if they are checked or not. How can I make an exception for check boxes, or is there an other way to do this? The datagridview is set to autogenerateColumns = true. Groover,
0200 A9 23 0202 8D 01 80 0205 00
-
You could test the
DataGridViewCellFormattingEventArgs.DesiredType
property. It will betypeof(string)
for textbox cells andtypeof(bool)
for checkbox cells. Alan.Thanks Alan, Now I have it for both types in two lines:
private void dataGridViewData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if(e.DesiredType==typeof(string) && (!string.IsNullOrEmpty(e.Value.ToString())))
e.CellStyle.BackColor = Color.LightGoldenrodYellow;if(e.DesiredType==typeof(bool)&&((Convert.ToBoolean(e.Value) == true))) e.CellStyle.BackColor = Color.LightGoldenrodYellow; }
Edited: I did not test it thorough, if I insert a new row in database I get an exception:
if(e.DesiredType==typeof(bool)&&((Convert.ToBoolean(e.Value) == true)))
Object cannot be cast from DBNull to other types. Groover,
0200 A9 23 0202 8D 01 80 0205 00