I want to change my grid view row/cell background color depend on grid view cell or column value. I have a gridview asp.net (WebForms), one column which has quantity (int). depend on this value, every row background color should be change dynamically.
-
There's no `bootstrap gridview.` Styling like colors is controlled using CSS. Bootstrap makes this easier and *does* allow dynamic changes. What grid view component are you actually using? Which stack? WebForms? MVC? Razor Pages? It sounds like you're using Web Forms which *doesn't* work well with Bootstrap – Panagiotis Kanavos May 29 '23 at 09:11
-
A gridview, with cssClass="table" MOST CERTAINLY results in a HTML table formatted with the table bootstrap table class. Thus it quite common and fair to state one has a table that is a bootstrap formatted table. I suppose we are getting twisted up in semantics here, but a gridview, or even a listview set to boostrap class "table" does result in a bootstrap formatted HTML table - and this is the case even when using a GridView. The output and result of the Gridview will result in a bootstrap table that is even responsive (will re-size based on screen size). What would you call such a table? – Albert D. Kallal May 30 '23 at 01:27
2 Answers
//In asp.net (WebForms) gridview <asp:GridView create an event
OnRowDataBound="GridView1_RowDataBound"
// then go to c# OnRowDataBound event and write
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label myQuantity= (e.Row.FindControl("myQuantity") as Label);
int Quantity= Convert.ToInt32(myQuantity.Text); // you can change datatype
if(Quantity >5 ) //you can put your require value
{
e.Row.BackColor = System.Drawing.Color.AliceBlue;
}
}

- 17
- 4
-
Not really. There's no `bootstrap grid view` to begin with. In all modern web sites styling is controlled using CSS instead of directly modifying HTML. Bootstrap is a way to make CSS styling easier. What you posted here is direct modification of the HTML output that wasn't needed even in WebForms – Panagiotis Kanavos May 29 '23 at 09:09
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 29 '23 at 21:05
-
@PanagiotisKanavos -- You going to conditional format the color of a row based JUST on pure css here? Really? Using System.Drawing.Color results in a HTML color code - the SAME one css is going to use!!! The poster is free to set the row style or the back color. The HTML rendered will produce a HTML color anyway. I VERY much doubt that one would use css alone to conditional set the colors of the row based on a row value. While css can now have variables, it is predominately a declarative system, not a procedural language, and this problem is more procedural then a declarative type of problem. – Albert D. Kallal May 30 '23 at 01:36
-
@PanagiotisKanavos -- After seeing my post below, can you share how your going to format each row with css and not use code? This I have to see!!! As you can see, we applied the bootstrap class "table" and "table-hover" to get a nice looking table, and better yet, it also responsive as the resizing example shows. Not really sure how or what you getting your knickers all twisted up about some magic reason that we have to use css here, but as the example shows, setting the back ground color property of the row, or setting the cssClass for the row quite much produces the same result. – Albert D. Kallal May 30 '23 at 02:25
-
@AlbertD.Kallal I was ready to reply "read Albert Kallal's answer" but now I realize ... you're attacking yourself. I didn't write you can use pure CSS, but I suspect it's doable even with an ancient grid. You can assign a different class per row, esp. if you use templates. You can use different classes based on the data item. GridView is certainly harder to use than any other grid component since the MVP program used that old logo but you *can* do it. If it doesn't allow using a row template you can write a CSS selector that selects the row based on a cell's value. – Panagiotis Kanavos May 30 '23 at 06:34
-
You suggesting to use a row selector and modify the DOM AFTER the HTML been generated then? Did you not just suggest how silly it is to modify the HTML in your narrative? Why generate the HTML, and THEN modify the HTML/DOM AFTER the fact? Why and how does that make sense? (and worse you suggesting to not modify the HTML, but that's exactly what a jQuery selector does to change the class settings DIRECTLY against the produced markup! In this case, by setting the background color, we are in fact TELLING the asp.net processor to produce correct HTML output in the first place - a huge difference! – Albert D. Kallal May 30 '23 at 14:04
-
Same goes for the style example which tells asp.net output processor to generate plain jane "tr" row with the correct css class setting. We not modifying the dom/html, but telling asp.net processor to GENERATE the correct HTML in the first place WHICH THEN DOES NOT need to be modified with some DOM selector that now has to parse the whole DOM again, and worse yet you have to include client side JavaScript and code to do that. Don't say we modifying the HTML, WHEN YOUR approach actually does that. This case we don't modify HTML, we get asp.net to generate the correct HTML in the first place. – Albert D. Kallal May 30 '23 at 14:09
-
EVEN if you did't say to use css to do this, you MOST certainly stated to not modify the HTML, but that is exactly what a jquery or JavaScript selector does!!! It will have to parse out the WHOLE dom, select the element(s), then modify them, and does so after the fact. Why not generate correct HTML output in the first place, and that is EXACTLY what our example does!!! Your standing here say don't directly modify the HTML, but THEN turn around and suggest a solution that does EXACTLY what your suggesting not to do!!! Setting properties of GV means HTML output is generated correctly. – Albert D. Kallal May 30 '23 at 14:13
-
We not modifying the HTML in this case (what jQuery and selectors would do). We telling the system to OUTPUT the correct HTML in the first place. So, any browser don't care that we used a older GridView, since the final output is plain jane HTML table markup. But at least in our case, we generated the table with the css settings already in place and NOT having to modify the output HTML. However, your approach means we ARE IN FACT MODIFYING the HTML AFTER the fact its been generated!!. By setting the class or even background of the row object, then the HTML does not require modifications. – Albert D. Kallal May 30 '23 at 14:18
-
So, WHERE in this code example are we modifying the HTML here? We are not!! The asp.net controls on a page are NOT html, but will be processing by asp.net INTO standard HTML5 compliant markup. So, in a ironic twist, the solution here does not modify the HTML directly such as what jQuery would do!! (and what YOU are suggesting to do!!!). We are generating the correct HTML output in the first place, and that is a MASSIVE difference from what you are suggesting here. – Albert D. Kallal May 30 '23 at 14:28
-
And how is the working example HARDER then using selectors against the HTML that "already" been generated? The simple matter is on row bound, we enjoy the ability to change the row propetties and do so BEFORE the HTML will or has been generated. So, how was ONE line of code oh so hard of a code approach? but worse, not only is the example here dead simple, it ALSO means we don't have to modify the HTML AFTER the fact, just like you suggested to not do, but then turn around and suggest a approach that MOST certainly does require one to directly modify the HTML after its been generated. – Albert D. Kallal May 30 '23 at 14:32
-
And if you have a solution with less effort and code, then it should be VAST easy to post such an example, right? – Albert D. Kallal May 30 '23 at 14:35
Let's build a gridview but use a Bootstrap table class. The result will be a HTML bootstrap class table.
So, we have this markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" Width="50%">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server"
Checked='<%# Eval("Active") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And it looks like this:
Now, that is a rather ugly looking table.
So, let's add a Bootstrap "table" class, and like magic, we get a nice formatted table.
So, the gv markup is now this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" Width="50%"
CssClass="table table-hover"
The rest remains the same!
The result now is a nice Bootstrap formatted table.
We get this:
Note how very nice the resulting gridview table is - it's now a Bootstrap formatted table, and one that as above shows, even re-sizes very nicely.
So now that we have that formatted Bootstrap table, the next goal is to change the color of each row.
Let's format each row as skyblue for when the column is Active.
As suggested, the common approach is to use the row data bound event.
So, we now have this:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView gData = (DataRowView)e.Row.DataItem;
if ((bool)gData["Active"])
e.Row.BackColor = System.Drawing.Color.LightSteelBlue;
}
}
And the result is this:
And I suppose for fun, you can assign the row a CSS attribute, or even a class.
So then this:
if ((bool)gData["Active"])
e.Row.Style.Add("background-color", "LightSteelBlue");
The result will be the same effect as the first example code.

- 19,824
- 17
- 99
- 186

- 42,205
- 3
- 34
- 51