By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

DatagridView Formatting Issue

P: n/a
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;
}
}
}
Jun 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" wrote:
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;
}
}
}
Jun 5 '07 #2

P: n/a
Thank you Sergey, that is what I needed.

"Sergey Poberezovskiy" wrote:
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" wrote:
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;
}
}
}
Jun 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.