更改gridview行颜色 [英] Changing gridview row color

查看:80
本文介绍了更改gridview行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的gridview如下所示..我需要根据db中的颜色值更改行颜色。



//ASAS.NET

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


//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();
       }



//使用Id获取颜色的存储过程


//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

推荐答案

使用 GridViewRowEventArgs e 传入 GridView1_RowDataBound 程序:

Use GridViewRowEventArgs e passed into GridView1_RowDataBound procedure:
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.BackColor = Color.FromName(StringValueReturnedByQuery);
        }





但是之前......看看下面SP:



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


你好,



OnDataRowBound事件你可以设置科洛根据你的情况行的行数。



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


这篇关于更改gridview行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆