Hi I am facing problem in changing datagridview rows color based on data. but in my datagrid last row containing total so I don't want to apply any background color on it.
I sent the cor or cellRormatting event of datagridview
Here I am using (e.RowIndex!= dgvRoomTypeSeven.Rows.Count-1) to check that it is last row.
but e.RowIndex always gives 0 why?
private void dgvRoomTypeSeven_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.ColumnIndex != 0 && e.RowIndex!= dgvRoomTypeSeven.Rows.Count-1)
{
if (e.Value.ToString() == "N/A") e.CellStyle.BackColor = Color.Red;
else
e.CellStyle.BackColor = Color.Green;
}
}
Is there any other way?
Loading

Anil KumarPosted Apr 24, 2012, 2:20 AM
check the row type at the RowDataBound and set the color.
if (e.Row.RowType == DataControlRowType.Footer)
Please check the event you are recieving. Proper event will give you the proper index value. For example if you opt above method, you can recieve events in GridViewRowEventArgs e
Vikas AhlawatPosted Apr 24, 2012, 5:30 AM
I am following second method.
But if any one have the answers of my first question(e.Rowindex 0 why)
then please reply.
Anil KumarPosted Apr 24, 2012, 2:26 AM
Also check your own method by changing event to DataGridViewCellEventArgs
Vikas AhlawatPosted Apr 24, 2012, 2:08 AM
I want to know that why e.RowIndex gives 0 every time.
Because I want to apply it on CellFormatting event.
or can you tell me any other event which is better for this task (applying loop).
Anil KumarPosted Apr 24, 2012, 1:45 AM
You can use GridViewRow after binding your GridView and set the color as -
foreach (GridViewRow row in myDataGridView.Rows) )
{
if (
{
row.BackColor = Color.Red;
}
}
Or check the row type at the RowDataBound and set the color.
if (e.Row.RowType == DataControlRowType.Footer)