DatagridView Formatting Issue

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

Guest

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.
 
G

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with DataView.Sort 2
Re. Updating DataViews 1
distinct 3
Distinct groupid 1
Issue with DataView and DataRowView 1
Looping through related data..? 2

Top