【问题标题】:Error when I set the color of an GridView Row when a column has a specific value当列具有特定值时设置 GridView 行的颜色时出错
【发布时间】:2012-02-10 10:53:41
【问题描述】:

如果特定列有值,我想设置 gridview 行颜色。但是我在我的 DIVSTATUS 中收到关于 nullreference 的错误。

我的 ASPX

        <asp:TemplateField HeaderText="Status">
            <ItemTemplate>                
                <div style="width:70px;" id="divStatus" runat="server"><%# Eval("DscStatus")%></div>
            </ItemTemplate>
        </asp:TemplateField>

我的代码隐藏

                if (GridView1.Rows.Count > 0)
                {
                    for (int i = 0; i < GridView1.Rows.Count; i++)
                    {
                        HtmlContainerControl divstatus = (HtmlContainerControl)GridView1.Rows[i].FindControl("divstatus");
                            if (divstatus != null)
                            {
                                if (divstatus.InnerText == "Andamento Project")
                                {
                                    GridView1.Rows[i].BackColor = System.Drawing.Color.Navy;
                                    GridView1.Rows[i].ForeColor = System.Drawing.Color.White;
                                }
                            }
                    }
              }

我的渲染 HTML

<td>
     <div style="width:70px;">Andamento Project</div>
</td>

【问题讨论】:

  • 我认为你应该在Row Databound事件中设置它们

标签: c# asp.net gridview


【解决方案1】:

利用RowDataBound 属性并执行如下代码...您不需要获取 div 状态,您只需获取您的数据类型,在该数据类型中,您可以通过使用属性值来获取您的属性值决定你行的颜色

protected void grdCAPRate_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       yourtype obj=  (yourtype)e.Row.DataItem;

           if (obj.DscStatus == "Andamento Project")    
            e.Row.BackColor = System.Drawing.Color.Navy;
          else
           e.Row.ForeColor=System.Drawing.Color.White;                                 

     }                
}

【讨论】:

  • 当我的用户单击按钮时,我调用我的“loadGrid”方法。在这种方法中,我放置了我的代码。如果没有 RowDataBound 事件,我该怎么做?使用 RowDataBound 我认为会非常慢,因为我的 GridView 中有很多数据。
  • @Lucas_Santos - 嘿,如果可能的话,你的代码还可以使用标签控件......并设置标签的文本值而不是 div ..
  • 我会尝试使用标签。让我试试。
  • 不起作用。我认为这是因为我在我的 GridView 上进行了数据绑定,并以同样的方法尝试找到标签的值。唯一的方法是通过 OnRowDataBound ?
  • @Lucas_Santos - 嘿,我不太了解您的代码,但我认为在这种情况下最好使用 rowdatabound
【解决方案2】:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HtmlContainerControl divstatus = (HtmlContainerControl) e.Row.FindControl("divstatus");
            if (divstatus != null)
            {
                if (divstatus.InnerText == "Andamento Project")
                {
                    e.Row.BackColor = Color.Navy;
                    e.Row.ForeColor = Color.White;
                }
            }
        }
    }

【讨论】:

  • 当我的用户点击按钮时,我调用我的“loadGrid”方法。在这种方法中,我放置了我的代码。如果没有 RowDataBound 事件,我该怎么做?使用 RowDataBound 我认为会很慢,因为我的 GridView 中有很多数据。
  • 我试过你的代码。您的代码运行没有错误。您可以使用 Debug 正确检查您的代码。一定有什么被忽略的。
猜你喜欢
  • 1970-01-01
  • 2015-12-28
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
相关资源
最近更新 更多