Setting Gridview color and font from database value

Donald Symmons 2,856 Reputation points
2024-04-07T20:50:54.3466667+00:00

I am trying to set color in my gridview grid lines and also set font in my gridview from a database value. I tried a workaround is to do this by using the GridView's RowDataBound event but the color value from database did not apply in the gridview. At the moment, the font is applied when I navigate to the page, but when I click on a button on that page, the font changes to the font of the page

Please how do I do this?

This is where I set the color in the OnRowDataBound handler method:

protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            foreach (TableCell Gridtc in e.Row.Cells)
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM DesignTable WHERE Id = @Id", con))
                    {
                        cmd.Parameters.AddWithValue("@Id", Session["user"]);
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            string Colorlbl = dr[7].ToString();
                            Gridtc.Attributes["style"] = "color: \'" + Colorlbl + "\';";
                            string LabelFont = dr[6].ToString();
                            Page.Header.Controls.Add(new System.Web.UI.LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\" https://fonts.googleapis.com/css?family=" + LabelFont + "\" />"));
                            Gridtc.Attributes["style"] = "font-family: \'" + LabelFont + "\';";
                        }
                        con.Close();
                    }
                }
            }
        }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,395 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,270 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,636 Reputation points Microsoft Vendor
    2024-04-08T06:31:16.7766667+00:00

    Hi @Donald Symmons,

    Page.Header.Controls.Add needs to be used in the Page_Load method. I think you can try to complete all the requirements in Page_Load.

    You can use BackColor to set Gridview color.

    Demo

     protected void Page_Load(object sender, EventArgs e)
     {           
         string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
         SqlConnection con = new SqlConnection(constr);            
         SqlCommand cmd1 = new SqlCommand("SELECT * FROM DesignTable WHERE Id = @Id", con);
         cmd1.Parameters.AddWithValue("@Id", Session["user"]);
         con.Open();
         SqlDataReader dr = cmd1.ExecuteReader();
         if (dr.Read())
         {
             string Colorlbl = dr[1].ToString();
             Color color = Color.FromName(Colorlbl);
             MyGrid.BackColor = color;
             string LabelFont = dr[2].ToString();
             Page.Header.Controls.Add(new System.Web.UI.LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\" https://fonts.googleapis.com/css?family=" + LabelFont + "\" />"));
             MyGrid.Attributes["style"] = "font-family: \'" + LabelFont + "\';";
         }
         con.Close();
         if (!this.IsPostBack)
         {
             SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers");
             SqlDataAdapter sda = new SqlDataAdapter();
             cmd.Connection = con;
             sda.SelectCommand = cmd;
             DataTable dt = new DataTable();
             sda.Fill(dt);
             MyGrid.DataSource = dt;
             MyGrid.DataBind();
         }
       
     }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. SurferOnWww 1,911 Reputation points
    2024-04-08T01:02:13.04+00:00

    Please note that the RowDataBound fires for all the rows in GridView including header, data and footer.

    If the GridView have thousands rows the RowDataBound fires thousand times. In your code above, the select query will be passed to SQL Server thousands x thousands times. That is not what you want, I believe.

    I suggest you include the style information in the data that is bound to the MyGrid and obtain the style information from the DataRowView something like below:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
    
            string Colorlbl = (string)drv["Color"];
            string LabelFont = (string)drv["Font"];
        } 
    }
    
    0 comments No comments