Click here to Skip to main content
Rate this: bad
good
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.NET
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
//C#.NET
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        SqlConnection Connection = new SqlConnection("conn string");
        Connection.Open();
        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;
        com.ExecuteNonQuery();
        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);               
            }
            con.Close(); 
        }
 
//STORED PROCEDURE to get color using Id
CREATE proc [dbo].[gridcolor] @colorId bigint,@color nvarchar(40) output
as
select @colorstatusColor=color from status where colorId=@colorId
Posted 2-Apr-13 21:26pm
Edited 2-Apr-13 21:27pm
v2
Comments
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
good
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
AS
BEGIN
    SELECT @color=color
    FROM status
    WHERE colorId=@colorId
 
    RETURN @color
END
  Permalink  
Comments
Janani Muthaiyan at 3-Apr-13 4:16am
   
@Maciej Los, Its getting the color from DB for the first time only..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello,
 
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");
            }
        }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 3 Apr 2013
Copyright © CodeProject, 1999-2014
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