Click here to Skip to main content
15,912,457 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Members,

I have a grid view that is populated dynamically through a stored procedure

I would like to ask how can I set the colour of the value for column17 upon populating the gridview?
For example, for value range between 1 - 20 I would like to the colour to be green, value between 21-40 to be orange and 41 - 60 to be red?


My codes to populate the gridview:
C#
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
                    {
     cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = DATE;
                            cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = Key;
    string query = cmd.CommandText;
    
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    Gridview1.DataSource = ds.Tables[0];
    Gridview1.DataBind();
Posted
Updated 26-Oct-15 0:53am
v3

Hello Permalink,

Its very easy with RowDatabound event of the Gridview Controll

Select The Grid View Control =>Properties=>events=>rowDatabound double click and past the code below :

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int iCellValue=Convert.ToInt32(e.Row.Cells[16].Text);
if (iCellValue >= 1 && iCellValue <= 20)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Green;

}
else if (iCellValue >= 21 && iCellValue <= 40)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Orange;

}
if (iCellValue >= 41 && iCellValue <= 60)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Red;

}
}
}
 
Share this answer
 
I had done this in my GridView

XML
<asp:TemplateField HeaderText="Correct Filling No">
                                            <ItemTemplate>
                                                <asp:Label ID="Label10" runat="server" Text='<%# Bind("CorrectFillingNo") %>'></asp:Label>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <asp:DropDownList ID="ddlcorrectFillingNo" runat="server" SelectedValue='<%# Eval("CorrectFillingNo").ToString()=="" ? "Yes" : Eval("CorrectFillingNo") %>'
                                                    CssClass="textareadropdown" BackColor='<%# Eval("CorrectFillingNo").ToString()=="No" ? System.Drawing.Color.Red : System.Drawing.ColorTranslator.FromHtml("#F3F3F3")  %>'>
                                                    <asp:ListItem Selected="True">Yes</asp:ListItem>
                                                     <asp:ListItem>No</asp:ListItem>
                                                    <asp:ListItem>N/A</asp:ListItem>
                                                </asp:DropDownList>
                                            </EditItemTemplate>
                                        </asp:TemplateField>
 
Share this answer
 
Hi,

OnRowDataBound event you can make the color against cell using conditions, Refer below sample.

C#
protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
     if(e.Row.RowType==DataControlRowType.DataRow)
     {
          int value=Convert.ToInt32(((Label)e.Row.FindControl("lblValue")).Text); // If you are using a TemplateField
          

          if(value > 1 && value < 20 )
          {
              e.Item.BackColor = System.Drawing.Color.Green;
          }
          else if(value > 21 && value < 40 )
          {
              e.Item.BackColor = System.Drawing.Color.Orange;
          }
          else if(value &gt; 41 &amp;&amp; value &lt; 60 )
          {
              e.Item.BackColor = System.Drawing.Color.Red;
          }

     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900