i have a datageidview which supposed to color the rows with contains specific value
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow myrow in dataGridView2.Rows)
{
if (e.RowIndex != -1)
{
if (myrow.Cells[7].Value.ToString() == "Error")
{
myrow.DefaultCellStyle.BackColor = Color.Red;
}
else if (myrow.Cells[7].Value.ToString() == "NoError")
{
myrow.DefaultCellStyle.BackColor = Color.Green;
}
}
}
}
but i have a problem when the first row contains this value all the rows is colored with it's color
any help ??
The CellFormatting event is sent for all visible cells in the grid. You may have better luck using the data given in the event to change the color.
private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex != -1)
{
if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "Error")
{
e.CellStyle.BackColor = Color.Red;
}
else if (dataGridView2.Rows[e.RowIndex].Cells[7].Value.ToString() == "NoError")
{
e.CellStyle.BackColor = Color.Green;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.