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.

58 Comments

  • 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.|

  • 13 online dating sites Tips for Women

    After 15 years of capturing of people to use for their Online dating profiles, They still ask me exactly the same irritating questions before the photo shoot begins. Some of charmdate reviews the actual requests are as follows: 1) Can you might be me look thin? 2) Can you’re me look 20 years younger? 3) Can you airbrush all of my lines? 4) is it possible add hair to my head? 5) Can you make me appear as if a model? in my view, one of many reasons for getting your picture taken is so that people will recognize you when they meet you in person. If you chat with someone Online for a short time and eventually agree to meet, How is it supposed to work out when your profile pictures look hardly anything like you? Based on my own experiences with online dating, As well as my exposure to photographing people for their Online dating profiles, Here are 13 tips that I wants to share with you regarding your Online dating profile. (Although these tips are mainly for single women from a single man s outlook, Men can probably glean a few things from them, perfectly.) 1) Add a current picture of yourself to your profile. By today’s, I mean a photo that was taken today. If you can t afford to have consultant pictures taken, Even a selfie will suffice if the lighting is decent. Just it is advisable to take head shots as well as full length photos with a selfie stick or just hand your phone to a friend and ask him/her to take a few quick pictures of you. 2) Never post a graphic of a picture. The coiffure and wardrobe from 1979, as well as flash reflecting off of the print, Make it painfully obvious that your picture was taken many years ago and that you don t like any of your current pictures. 3) Never use a group photo as much of your profile picture. How is your prospective future soulmate supposed to figure out which person you are? Avoiding using lots of group pictures, more often than not, Is a better plan. 4) Take a moment to answer the basic questions in your profile. I generally ignore or block women who write me if they haven t bothered to even fill out the standard information in their profiles. I don t have time to ask each one of them a lot of questions just to view if we have anything in common. those actions should already be addressed when I read their profiles, which includes marital status, puffing habits, drinking habits, quantity of pets at home, And number of youngsters at home. 5) Never post a picture of yourself that have been digitally altered. These transitions are usually very obvious. and also, When you meet someone in person and that person sees the real you, Those airbrushed wrinkles and extra pounds are going to stand out even more. 6) Don t lie in your guide. if you are a chain smoker, for example, Do not confirm the non smoker box in your profile. accept it or not, I ve actually seen quite a few profiles of women broke but pictures of them smoking while claiming to be non smokers. It s preposterous. If a woman has no problem lying about something as severe as smoking, It makes me wonder what other things she has lied about and continues lying about. It also makes me wonder why she is so more comfortable with lying, more often than not. 7) Choose a username that says something both interesting and true about you. as an example, I ve run across women s profiles with usernames like SmilesAlways721 or AlwaysSmiling823 and a bunch of their pictures show them with big frowns on their faces. How can anyone bring them seriously? 8) Avoid posting multiple online dating sites profiles on the same site. It may sound preposterous but I have actually seen one woman post about a dozen profiles on the same site with slightly different usernames but with just one pictures. It makes her look certainly desperate. 9) avert aerials. If all of a woman s shots, rather tight selfie head shots, Are removed from way above her head, And she has no full length shots, It seems obvious to me that she is searching hide her obesity. 10) Avoid covering. I have seen so many women hiding their bodies behind trees, seats, dogs, and other people in their pictures. the road I see it, If a woman is extremely overwieght, She needs to honest and open about it in her profile. With all of the millions of single families, I would think that at least under,a number of might be okay with, Or even fascinated by, Big women. I don t see how seeking to deceive people, although, Could ever possibly exercise session. 11) Avoid posting pictures of stuffs that aren t you, particularly in your main profile picture. Too many women make the mistake of making their residence, motor vehicle, dog, Or even additional random person their main profile picture. With millions of other Online dating profiles to go through, Who has time to sift through pictures of dogs, trucks, herbs, people’s homes, And other random things to find the one picture of the one that wrote the profile? 12) Avoid posting pictures of yourself that have been taken in 1986. This is related to the first tip that I mentioned. although it seems outlandish, I ve actually run across these types of thing quite a few times. One woman’s, as an example, Posted pictures of herself that were taken many years historically. After chatting with her Online for calendar months and then on the phone for several more weeks, We agreed to meet for a casual sit down elsewhere somewhere but I was unable to recognize her when she showed up. She looked little or nothing like her picture. Her considerable, Curly black hair was now very instant, symmetrical, And us platinum blonde, She had purchased an extra 100 pounds or so, and he or she had aged about a dozen years. She walked even me, though, And announced herself. unquestionably, She recognized me because I actually might look like my picture. I left and never talked to her again. The horrible experience was extremely disappointing, galling, And a total waste of my time. 13) get your address right. Too over and over, I run across women who claim to exist in Houston, TX but after chatting with them I find out that they live in some city hundreds or even thousands of miles away. needlessly to say, I block them after I reduced lie. websites that provide in Houston, Then put that in your user profile. living in Austin, though, Put that in your concern. that way, Nobody has their time burned. On many dating site, You can actually enter your exact zip code so that people who live nearby could be more inclined contact you. I hope these tips help you with your online dating service experience.

  • Hey there! Someone in my Facebook group shared this site with us so I came to give it a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Excellent blog and superb style and design.

  • Thanks for your marvelous posting! I actually enjoyed reading it, you happen to be a great author. I will be sure to bookmark your blog and will often come back later in life. I want to encourage you to continue your great posts, have a nice evening!|

  • Dating getting married to Asian Girls in Western Nations

    then again, They reach the house and they are soft Asian wives who respect their husbands. They do not discuss or do not discuss extremely with their husbands the home. The Asian girls want to prevent a family happy. They can be sacrificed for their children. They are laid out with being to the bottom a little so that their children are upwards. At all functions, The Western men or the Asian men who married with the Asian women of dating must love their wives a growing number of to deserve what they do for you.

    The Asian women more respect their husband than other ladies on the world. The Asian girl I dated with become my wife now. When I arrive at the house of work, It takes our children at the house and prepares the dinner for the family. When I try to help it with some races in the kitchen area such as the dishes of washing, She denied my company. she wants to make all alone. Day laborer it arrives at the house earlier than I make thus it take your kids at the house and prepare the dinner. When I arrive at the house, I observe just exposures of TV while it prepare the dinner. She does not want only I help her either. we don’t enter arguments. When I become annoyed, It is secluded. This induces me to cool too. hence, I am happy to be as a my best wife, An wok cookware wife.

    When I left with the Asian girl the first date, She sometimes did not answer my questions when I gave him some adds to about his beauty. She smiled instead of answered my questions. She encouraged me to recall me before I was 13 or 14 years talking to a girl inside the first time. She allowed me to feel me young person. you will find other questions which she did not answer of the whole. She smiled or inclined the head her head as answering my questions. I imagine that the Asian girls are quiet. Our first date where we ate in a cafe, I spoke by posing much about uncertainties, It just spoke a little while answering some about my questions. She smiled or inclined the head her head more than she spoke or posed to me of concerns.

    The Asian girls of love of reason I in the Western countries is this can softness and nicest. The Asian women are held outside of a group of Western girls. A reason is the actual Western women are large and large and strong. The Asian girls date chinese women are compact, thin out, And exhausted. little the, flat one, And weak figure of an Asian lady support the Western men to fall on them. everyone loves them each time I see an Asian female walking on the street, At the local mall, Or everywhere in a different. I am a Western type which lives in texas, the us, I saw many Asian girls at the Asian demand here. i adore them. I make cling to an Asian friend upwards with a girl. I liked it at the start our first date. There are some single things that we like them at the start.

  • Do you mind if I quote a few of your articles as long as I provide credit and sources back to your website? My website is in the exact same niche as yours and my users would really benefit from some of the information you present here. Please let me know if this okay with you. Thanks!|

  • You’re so cool! I do not believe I’ve truly read through something like that before. So great to discover someone with original thoughts on this topic. Seriously.. thanks for starting this up. This site is one thing that’s needed on the internet, someone with a bit of originality!|

  • Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My website is in the very same niche as yours and my users would genuinely benefit from some of the information you present here. Please let me know if this okay with you. Many thanks!|

  • Hello, i think that i saw you visited my site thus i came to “return the favor”.I am attempting to find things to enhance my website!I suppose its ok to use a few of your ideas!!

  • Heya i am for the first time here. I found this board and I in finding It truly helpful & it helped me out much. I am hoping to provide one thing again and aid others like you aided me.

  • 4686 722115You should participate in a contest for among the top blogs on the internet. I will suggest this web site! 123678

  • Wow, this paragraph is good, my sister is analyzing these kinds of things, so I am going to let know her.|