This article will help you if you wish to color cells of Gridview and learn how to access cell values.
This article is all about how to set or change the background \color of the selected row of ASP.Net GridView programmatically.
Here we go with the cs file code
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
namespace gridcolor {
public partial class WebForm1: System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
DataTable dt = new DataTable();
dt.Columns.add("Hobbies");
//here only one column for easy understanding
dt.Rows.add("I love C# corner")
mygrid.datasource = dt;
mygrid.databind();
//for perticular cell //here it has only 1 cell (0,0)
myGrid.Rows[i].Cells[j].Style.BackColor = system.drawing.color.Red;
//incase you want to fill all rows
for (int i = 0; i < myGrid.Rows.Count; i++) {
for (int j = 0; j < myGrid.Rows[i].Cells.Count; j++) {
myGrid.Rows[i].Cells[j].Style.BackColor = /*color you want*/
}
}
}
}
}Now a brief look to my aspx
page code
HTML / XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="Hobbies" HeaderText="Hobbies" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
</form>
</body>
</html>Hope you would easily understand the tutorial :-)
Sharing is caring. Share with your coding buddies.


Ahmed KhalidPosted Nov 14, 2022, 3:29 PM EST
HI abishek, what if i have a column in data grid that is being populated by item from a dropdown list in a form. And i want to explicitly highlight a row containg that specific list item. The itel list is all string data. How do i do that?
gibib khanPosted Oct 25, 2022, 10:04 PM EDT
Amzing men!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Ronny FernandezPosted Oct 20, 2022, 2:35 AM EDT
This is very understandable thankyou Mr. Abhishek
Iswer reddyPosted Oct 19, 2022, 9:54 PM EDT
Good going abhishek
sweeta pandeyPosted Oct 19, 2022, 8:58 PM EDT
Thanks it saved my job
harshita singhPosted Oct 19, 2022, 12:15 PM EDT
Please provide me code sample
Saket SinghPosted Oct 19, 2022, 11:58 AM EDT
Code is working thanks a lot
Abhishek YadavPosted Oct 18, 2022, 8:59 PM EDT
thanks David
David YadavPosted Oct 18, 2022, 8:24 AM EDT
it works thanks 😁