Hello Friends, In this tutorial, I will show you how to change DataGridView Row Color depending on the cell value. Suppose

How to Change DataGridView Row Color depending on the cell value in C#.NET:

Suppose I have a set of Employee Records having status Active and Inactive and depending on the status of an employee I want to show different Row color in DataGridView based on the status of an employee. For example, if an Employee Status is Active then I want to show Row Color to YellowGreen and for Inactive Employees, I want to show row color to Red. Let’s see how to change DataGridView Row color depending on the cell value.

Create Database and Table in SQL Server:

CREATE DATABASE [Debugonweb]

USE [Debugonweb]


GO

CREATE TABLE [dbo].[Employee](
[EmployeeID] [bigint] IDENTITY(1,1) NOT NULL,
[EmployeeName] [varchar](50) NULL,
[Gender] [varchar](10) NULL,
[Address] [varchar](50) NULL,
[State] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Status] [varchar](20) NULL,
CONSTRAINT [PK__Employee__3214EC274AA9A371] PRIMARY KEY CLUSTERED 
(
[EmployeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Insert Records:

USE [Debugonweb]
GO
SET IDENTITY_INSERT [dbo].[Employee] ON

GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (1, N'Vinod', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (2, N'Manoj', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (3, N'Ansh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (4, N'Ritesh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (5, N'Vishwas', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (6, N'Ansh', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (7, N'Varali', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (8, N'Nitin', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (9, N'Vikas', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (10, N'Ritwik', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (11, N'Arohi', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
INSERT [dbo].[Employee] ([EmployeeID], [EmployeeName], [Gender], [Address], [State], [City], [Status]) VALUES (12, N'Ketan', N'Male', N'Varanasi', N'UP', N'Varanasi', N'Active')
GO
SET IDENTITY_INSERT [dbo].[Employee] OFF
GO

Create a C# Windows Application:

Step 1: Create a C# Windows Application Project called HightLightRow.

Step 2: Drag a DataGridView on the Form.

Step 3: Double Click the Form and add the following namespaces on the Form1.cs class.

using System.Data;
using System.Data.SqlClient;

Step 4: Create a method that will Bind your Database Records to DataGridView.

public void BindDataGridView()
{
try
{
DataTable dtData = new DataTable();
using (SqlConnection dbCon = new SqlConnection("Data Source=your-data-source;Initial Catalog=Debugonweb;Integrated Security=true"))
{
using (SqlCommand cmdGetRecords = new SqlCommand("SELECT [EmployeeID],[EmployeeName],[Gender],[Address],[State],[City],[Status] FROM [dbo].[Employee]", dbCon))
{

if (dbCon.State == ConnectionState.Closed)
{
dbCon.Open();
}
using (SqlDataReader drGetData = cmdGetRecords.ExecuteReader())
{
dtData.Load(drGetData);
}
dataGridView1.DataSource = dtData;
}
}
}
catch (Exception ex)
{


}
}

Step 4: Call the above method on Form Load method.

private void Form1_Load(object sender, EventArgs e)
{
BindDataGridView();
 
}

Step 5: Now we will write the code for formatting rows depending on the cell value.

public void SetAlternateRowColor()
{
for (int iCount = 0; iCount < dataGridView1.Rows.Count - 1; iCount++)
{
if(Convert.ToString(dataGridView1.Rows[iCount].Cells[6].Value)=="InActive")
dataGridView1.Rows[iCount].DefaultCellStyle.BackColor = Color.Red;
else
dataGridView1.Rows[iCount].DefaultCellStyle.BackColor = Color.GreenYellow;

}
}

You can see in the aboce code we are setting different background color depending on the status of the employee.

Step 6: Call the SetAlternateRowColor() method on Form Load.

The complete code for Form1.cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace HightLightRow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
BindDataGridView();
SetAlternateRowColor();
}
public void BindDataGridView()
{
try
{
DataTable dtData = new DataTable();
using (SqlConnection dbCon = new SqlConnection("Data Source=DESKTOP-CN5IQI9\\SQLEXPRESS;Initial Catalog=Debugonweb;Integrated Security=true"))
{
using (SqlCommand cmdGetRecords = new SqlCommand("SELECT [EmployeeID],[EmployeeName],[Gender],[Address],[State],[City],[Status] FROM [dbo].[Employee]", dbCon))
{

if (dbCon.State == ConnectionState.Closed)
{
dbCon.Open();
}
using (SqlDataReader drGetData = cmdGetRecords.ExecuteReader())
{
dtData.Load(drGetData);
}
dataGridView1.DataSource = dtData;
}
}
}
catch (Exception ex)
{


}
}

public void SetAlternateRowColor()
{
for (int iCount = 0; iCount < dataGridView1.Rows.Count - 1; iCount++)
{
if(Convert.ToString(dataGridView1.Rows[iCount].Cells[6].Value)=="InActive")
dataGridView1.Rows[iCount].DefaultCellStyle.BackColor = Color.Red;
else
dataGridView1.Rows[iCount].DefaultCellStyle.BackColor = Color.GreenYellow;

}
}


}
}

Output:

DataGridView Row Color

View More:

Conclusion:

I hope you would love this post. Please feel free to comment for any technical help. Your feedback and suggestions are appreciated.

Thank You.

1 Comment

  • Just wish to say your article is as amazing. The clarity on your put up is simply nice and that i can suppose you’re an expert in this subject. Fine along with your permission let me to grab your feed to keep up to date with imminent post. Thank you 1,000,000 and please carry on the gratifying work.|