DatagridView Formatting Issue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello. My goal is to pain the entire row fore color as Red. If the value of
column 24 is true (boolean), I wish to paint the row as Red. Otherwise,
leave row as default (no formatting). The Datagridview datasource is a
datatable. Currently my issue is that all records are painted red on the
Cell_Formatting event. But not all records meet the filter condition. Any
ideas why? Here is code snippet

private void dgvPolicies_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataView view = (DataView)dgvPolicies.DataSource;

foreach (DataRowView rowView in view)
{
//if (rowView.Row.ItemArray[3].ToString().ToLower() == "test")
if (rowView.Row.ItemArray[24].ToString() == bool.TrueString)
{
e.CellStyle.ForeColor = Color.Red;
}
}
}
 
Toco,

In your formatting event you should not loop throgh all rows inside your
view - only use the one for the current row:
DataView view = (DataView)dgvPolicies.DataSource
if (view[e.RowIndex].Row.ItemArray[24].ToString() == bool.TrueString)
{
e.CellStyle.ForeColor = Color.Red;
}
otherwise all your cells will be red as long as there is at least one recods
that meets your condition.
 
Thank you Sergey, that is what I needed.

Sergey Poberezovskiy said:
Toco,

In your formatting event you should not loop throgh all rows inside your
view - only use the one for the current row:
DataView view = (DataView)dgvPolicies.DataSource
if (view[e.RowIndex].Row.ItemArray[24].ToString() == bool.TrueString)
{
e.CellStyle.ForeColor = Color.Red;
}
otherwise all your cells will be red as long as there is at least one recods
that meets your condition.

Toco said:
Hello. My goal is to pain the entire row fore color as Red. If the value of
column 24 is true (boolean), I wish to paint the row as Red. Otherwise,
leave row as default (no formatting). The Datagridview datasource is a
datatable. Currently my issue is that all records are painted red on the
Cell_Formatting event. But not all records meet the filter condition. Any
ideas why? Here is code snippet

private void dgvPolicies_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
DataView view = (DataView)dgvPolicies.DataSource;

foreach (DataRowView rowView in view)
{
//if (rowView.Row.ItemArray[3].ToString().ToLower() == "test")
if (rowView.Row.ItemArray[24].ToString() == bool.TrueString)
{
e.CellStyle.ForeColor = Color.Red;
}
}
}
 
Back
Top