【问题标题】:gridview label color changegridview 标签颜色变化
【发布时间】:2014-10-15 17:01:01
【问题描述】:
 protected void getmessagelisting()
    {    
          ds = new DataSet();
            SQL = "Select * from [magaj].[message] where UId='" + Session["UserID"].ToString() + "'  order by ID desc";
            SqlDataAdapter da = new SqlDataAdapter(SQL, _connect());
            da.Fill(ds, "message");
            Grid_Joblist.DataSource = ds;
            Grid_Joblist.Columns[0].Visible = true;
            Grid_Joblist.DataBind();
            Grid_Joblist.Columns[0].Visible = false;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["isread"].ToString() == "0")
                {
                    //  (e.Row.FindControl("lblsubject") as Label).ForeColor = Color.Yellow;
                    foreach (GridViewRow row in Grid_Joblist.Rows)
                    {
                        Label lblsubject = row.FindControl("lblsubject") as Label;
                        lblsubject.ForeColor = Color.Yellow;

                    }
                }
            }
}

现在当我运行它时,我将以正确的方式绑定所有数据 但是当我想做任何具有value of isread=0 的数据时,我想更改该标签的颜色。

但是当任何 1 行满足这个 if 条件时,它也会改变所有其他标签颜色..

如果标签颜色满足isread=0,我只想更改标签颜色,而对于其他不满足的数据,它将是不同的颜色

我该怎么做?

【问题讨论】:

  • 当网格被重绘时,为什么不在那里做你的颜色逻辑..?
  • 哪里可以写??

标签: c# asp.net .net gridview


【解决方案1】:

你可以做一件事:

只需在标签中打印您的 isread 值,例如:

<asp:Label runat="server" ID="lblisread" Text='<%#Eval("isread")%>' Visible="false"/>

然后改变你的功能如下:

   foreach (GridViewRow row in Grid_Joblist.Rows)
    {
        Label lblsubject = row.FindControl("lblsubject") as Label;
        Label lblisread = row.FindControl("lblisread ") as Label;

        if (lblisread..Text == "0")
        {
            lblsubject.ForeColor = Color.Red;

        }
        else
        {
            lblsubject.ForeColor = Color.Yellow;
        }

    }

它完成了......! 干杯..!!!

【讨论】:

    【解决方案2】:

    使用 GridView RowDataBound 事件来设置标签颜色。

    如下所示

    void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          System.Data.DataRow row = (System.Data.DataRow)e.Row.DataItem;
          if(row["isread"].ToString()=="0")
            {
    
               Control l = e.Row.FindControl("lblsubject");
      ((Label)l).ForeColor= System.Drawing.Color.Yellow;
    
        }
    
      }
    

    未经测试的代码。

    查看这些链接了解更多信息:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound%28v=vs.110%29.aspx

    Row background color in gridview?

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多