0

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>
2
  • Where is your code and a Minimal, Complete, and Verifiable example? What kind of Table are you talking about for example. Commented Feb 3, 2017 at 20:21
  • I've added some code @VDWWD Commented Feb 6, 2017 at 16:42

4 Answers 4

2

You should do the job in asp to create an REST API for this sql table. Then with Ajax & JS you could GET this values for each html table and save to variables.

And then with simple:

if (your_sql_value === "yes") {
    document.querySelector('.css-class').style.backgroundColor = "#ff0000"
} else if (your_sql_value === "no") {
    document.querySelector('.css-class').style.backgroundColor = "#00ff00"
}
Sign up to request clarification or add additional context in comments.

1 Comment

I actually need it for a grideview not table
1

From your answer, i'm assuming you're using an asp:Table.

So basically there are two ways this could go:

1. Your table is already filled and rendered

If this is the case, check this table example:

Image:

enter image description here

.ASPX Table Code:

<asp:Table ID="submitTable" runat="server" GridLines="Both">
    <asp:TableRow runat="server" HorizontalAlign="Center" TableSection="TableHeader" VerticalAlign="Middle">
        <asp:TableCell runat="server" Width="128px">ID</asp:TableCell>
        <asp:TableCell runat="server" Width="128px">Status</asp:TableCell>
    </asp:TableRow>
</asp:Table>

It is a table filled with random values of either "yes" or "no". In this situation, you could iterate through each row and simply apply the color attribute like this:

int statusColumnIndex = 1; // Because it is the second column

int k = 0;
foreach (TableRow row in submitTable.Rows)
{
    if(k != 0) // Do this so it won't color the header row
    {
        if( row.Cells[statusColumnIndex].Text.Equals("yes") )
        {
            row.Cells[statusColumnIndex].BackColor = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB
        }

        else
        {
            row.Cells[statusColumnIndex].BackColor = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB
        }
    }
    k++;
}

Result (different random values):

enter image description here

or

2. If you want to color the table as you fill the table

In this case, you could apply a color to it when you instantiate the TableCell objects to fill the TableRow:

string statusValueFromSql = valueFromSqlQuery;
Color color;

if( statusValueFromSql.Equals("yes") )
{
    color = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB
}
else
{
    color = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB
}

var cellStatus = new TableCell { BackColor = color };

The end result is exactly the same as situation 1.

I hope my answer helps.

Comments

1

You can use the OnRowDataBound event for that.

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

Code behind

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //check if the row is a datarow
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //cast the current row as a datarowview
        DataRowView row = e.Row.DataItem as DataRowView;

        //check the correct column value 
        if (Convert.ToBoolean(row["Status"]) == false)
        {
            //set the color of the row directly
            e.Row.BackColor = Color.Red;

            //or change it's class
            e.Row.CssClass = "Red";

            //or change a property per cell
            e.Row.Cells[0].CssClass = "Blue";
            e.Row.Cells[1].BackColor = Color.Purple;
        }
    }
}

Comments

1

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;
                }


            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.