In this article I will explain with an example, how to change DataGridView Row Color based on condition in Windows (WinForms) Application using C# and VB.Net.
Inside the CellFormatting event handler of the DataGridView, various conditions are used to compare the Cell value of the DataGridView Row and the Row color of the DataGridView is changed based on the conditions holding true.
 
 
Form Design
The Form consists of a DataGridView control.
Change DataGridView Row Color based on condition in Windows Application using C# and VB.Net
 
 
Populating the DataGridView
Inside the Form Load event handler, the DataGridView is populated with data by making use of a dynamic DataTable with some records.
C#
private void Form1_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Item"), new DataColumn("Quantity") });
    dt.Rows.Add("Shirt", 145);
    dt.Rows.Add("Jeans", 0);
    dt.Rows.Add("Trousers", 190);
    dt.Rows.Add("Tie", 30);
    dt.Rows.Add("Cap", 0);
    dt.Rows.Add("Hat", 90);
    dt.Rows.Add("Scarf", 290);
    dt.Rows.Add("Belt", 150);
    dataGridView1.DataSource = dt;
    dataGridView1.AllowUserToAddRows = false;
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim dt As DataTable = New DataTable
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Item"), New DataColumn("Quantity")})
    dt.Rows.Add("Shirt", 145)
    dt.Rows.Add("Jeans", 0)
    dt.Rows.Add("Trousers", 190)
    dt.Rows.Add("Tie", 30)
    dt.Rows.Add("Cap", 0)
    dt.Rows.Add("Hat", 90)
    dt.Rows.Add("Scarf", 290)
    dt.Rows.Add("Belt", 150)
    dataGridView1.DataSource = dt
    dataGridView1.AllowUserToAddRows = False
End Sub
 
 
Change DataGridView Row Color based on condition
Inside the CellFormatting event handler of the DataGridView, various conditions are used to compare the Cell value of the DataGridView Row and the Row color of the DataGridView is changed based on the conditions holding true.
The following Table shows the different quantity values and the corresponding color of the DataGridView Row.
Quantity Range
Background Color
0
RED
1 - 50
YELLOW
51 - 100
ORANGE
 
C#
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //Compare the value of second Column i.e. Column with Index 1.
    if (e.ColumnIndex == 1 && e.Value != null)
    {
        //Fetch the value of the second Column.
        int quantity = Convert.ToInt32(e.Value);
 
        //Apply Background color based on value.
        if (quantity == 0)
        {
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
        }
        if (quantity > 0 && quantity <= 50)
        {
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
        }
        if (quantity > 50 && quantity <= 100)
        {
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Orange;
        }
    }
}
 
VB.Net
Private Sub dataGridView1_CellFormatting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dataGridView1.CellFormatting
    'Compare the value of second Column i.e. Column with Index 1.
    If e.ColumnIndex = 1 AndAlso e.Value IsNot Nothing Then
 
        'Fetch the value of the second Column.
        Dim quantity As Integer = Convert.ToInt32(e.Value)
 
        'Apply Background color based on value.
        If quantity = 0 Then
            dataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red
        End If
 
        If quantity > 0 AndAlso quantity <= 50 Then
            dataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow
        End If
 
        If quantity > 50 AndAlso quantity <= 100 Then
            dataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Orange
        End If
    End If
End Sub
 
 
Screenshot
Change DataGridView Row Color based on condition in Windows Application using C# and VB.Net
 
 
Downloads