网格视图标签颜色更改

本文关键字:颜色 标签 视图 网格 | 更新日期: 2023-09-27 18:26:41

 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时更改标签颜色,而对于其他不满足此数据的数据,它将是不同的颜色

我该怎么做?

网格视图标签颜色更改

你可以做一件事:

只需在标签中打印您的 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;
        }
    }

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

使用

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

网格视图中的行背景颜色?