【问题标题】:How do I avoid a GridView's Gridlines changing color when applying a class to a cell on the RowDataBound event?将类应用于 RowDataBound 事件上的单元格时,如何避免 GridView 网格线改变颜色?
【发布时间】:2013-01-14 20:21:09
【问题描述】:

在我的网站中,我有一个带有 Gridview 的页面,用于显示一些数据。我捕获 RowDataBound 事件,以确定单元格中是否存在某些文本。如果是,我把它涂成绿色,否则我把它涂成红色。

问题是:Gridview 只有水平网格线。当我更改 RowDataBound 中单元格的颜色时(我实际上是在更改类),网格线采用应用的颜色。无论我尝试什么(循环遍历所有单元格并设置边框颜色),我都无法将其还原。请帮忙。

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 2; i <= 3; i++)
        {
            if (e.Row.Cells[i].Text.Contains("monkey"))
            {
                e.Row.Cells[i].Attributes.Add("class", "monkey bold");
            }
            else
            {
                e.Row.Cells[i].Attributes.Add("class", "nomonkey bold");
            }
        }
    }

}

样式如下:

.monkey
{
    color: #009900;
    border-color: black;
}

.nomonkey
{
    color: red;
    border-color: black;
}

border-color 属性好像没有效果。

GridView 定义为:

                        <asp:GridView ID="GridView2" runat="server" AllowSorting="False" GridLines="Horizontal" AutoGenerateColumns="false" CellPadding="4" OnRowDataBound="GridView2_RowDataBound" OnDataBound="GridView2_DataBound" CssClass="reportGrid"> 
                        <FooterStyle BackColor="#2F76B8" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#2F76B8" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                        <RowStyle BackColor="#FFFFFF" ForeColor="#222222" HorizontalAlign="Center" />

【问题讨论】:

    标签: c# html asp.net rowdatabound


    【解决方案1】:

    我在任何地方都找不到这个问题的答案,也无法诱使任何人回答它,所以我通过在单元格内添加一个跨度来解决它,并设置它的样式如下:

                if (e.Row.Cells[i].Text.Contains("monkey"))
                {
                    e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("monkey", "<span class=\"monkey\">monkey</span> ");
                }
    

    【讨论】:

      【解决方案2】:

      我也经历过同样的事情。另一种方法是添加一个 Label 控件并在其上设置属性。

      不要使用 BoundField,而是使用 TemplateField。假设您的数据返回一个可索引的项目:

      <asp:GridViewControl runat="server" ID="GridView2" AutoGenerateColumns="false">
          <asp:BoundField HeaderText="Field0" DataField="[0]" />
          <asp:BoundField HeaderText="Field1" DataField="[1]" />
          <asp:TemplateField HeaderText="Monkey1" />
          <asp:TemplateField HeaderText="Monkey2" />
      </asp:GridViewControl>
      

      然后在代码隐藏中:

      protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              var data_item = e.Row.DataItem; // Can use "as <type>;" if you know the type.
              if (data_item != null)
              {
                  for (int i = 2; i <= 3; i++)
                  {
                      var cell_content = new Label();
                      e.Row.Cells[i].Controls.Add(cell_content);
      
                      cell_content.Text = data_item[i];
                      if (data_item[i].Contains("monkey"))
                      {
                          cell_content.Attributes.Add("class", "monkey bold");
                      }
                      else
                      {
                          cell_content.Attributes.Add("class", "nomonkey bold");
                      }
                  }
              }
      

      当然,另一种选择是在 TemplateField -> ItemTemplate 声明中添加带有 ID 的标签并使用“Cells[i].FindControl("label_id")”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2010-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多