Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
I am having the gridview as follows.. I need to change the row color based on the color value in db.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        SqlConnection Connection = new SqlConnection("conn string");
        SqlCommand Command1 = Connection.CreateCommand();
        Command1.CommandText = "Select colorId from table";
        using (SqlDataReader reader = Command1.ExecuteReader())
           while (reader.Read())
                colorId= reader["colorId"].ToString();
        SqlCommand com = new SqlCommand("gridcolor", Connection);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@colorId", colorId);
        com.Parameters.Add("@color", SqlDbType.NVarChar, 30);
        com.Parameters["@color"].Direction = ParameterDirection.Output;
        string msg = (string)com.Parameters["@color"].Value;
            foreach (GridViewRow rows in GridView1.Rows)
               //here I need the condition to filter each rows with different color
               //  which I assigned in db
                GridView1.RowStyle.BackColor = System.Drawing.Color.FromName(msg);               
//STORED PROCEDURE to get color using Id
CREATE proc [dbo].[gridcolor] @colorId bigint,@color nvarchar(40) output
select @colorstatusColor=color from status where colorId=@colorId
Posted 2-Apr-13 21:26pm
Edited 2-Apr-13 21:27pm
Maciej Los at 3-Apr-13 3:33am
What are you trying to achieve? Do you want to set back color for row depending on values returned by query?
Janani Muthaiyan at 3-Apr-13 3:34am
yes absolutely
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Use GridViewRowEventArgs e passed into GridView1_RowDataBound procedure:
if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.BackColor = Color.FromName(StringValueReturnedByQuery);
But before... have a look at below SP:
CREATE proc [dbo].[gridcolor]
    @colorId bigint,
    @color nvarchar(40) output
    SELECT @color=color
    FROM status
    WHERE colorId=@colorId
    RETURN @color
Janani Muthaiyan at 3-Apr-13 4:16am
@Maciej Los, Its getting the color from DB for the first time only..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

OnDataRowBound Event you can set the color of row based on your condition.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Style.Add("background-color", "#000");

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

  Print Answers RSS
0 Maciej Los 495
1 OriginalGriff 400
2 Sergey Alexandrovich Kryukov 394
3 CHill60 320
4 Sascha Lefévre 269
0 Sergey Alexandrovich Kryukov 10,017
1 OriginalGriff 9,495
2 Peter Leow 5,241
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 3,076

Advertise | Privacy | Mobile
Web04 | 2.8.150327.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100