Parbhu Bissessar Parbhu Bissessar - 4 years ago 119
SQL Question

Change css value depending on the database value asp.net

Im using sql server and aspx. I want the value in the table to change color depending on the value in sql in the code behind file. So, if the value in one of the table == "yes" change the color #ff0000. If the value in the table is == "no" change it to #00ff00; Table is called: submitTable and the column is status. I'm using Gridview.

<asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " GridLines="None">
<Columns>
<asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" />
<asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" />
<asp:BoundField DataField="Status" HeaderText="Opened/Closed" HeaderStyle-CssClass="Color" />
</Columns>
</asp:GridView>

Answer Source

I've gotten it to work! This helped me too http://www.aspsnippets.com/Articles/Change-Background-color-of-GridView-Row-using-RowDataBound-event-in-ASPNet-using-C-and-VBNet.aspx

 <asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " OnRowDataBound = "OnRowDataBound">

//code behind

 protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCell cell = e.Row.Cells[0];  //the 0 changes depending on the column number in sql
                string status = (cell.Text);
                string yes = "yes";
                string no = "no";

                if (status.Equals(no))
                {
                    cell.BackColor = Color.Red;
                }
                if (status.Equals(yes))
                {
                    cell.BackColor = Color.Green;
                }


            }
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download