【问题标题】:Is it possible to set the background of a asp.net gridview cell?是否可以设置 asp.net gridview 单元格的背景?
【发布时间】:2012-09-04 17:14:16
【问题描述】:

有可能吗?我只能设置单元格的背景颜色。事情是,如果我设置背景颜色,它不会在您打印页面时显示(默认情况下)。所以我想通过 css 将背景设置为网格单元格,但在 BoundField.ItemStyle 类中找不到这个属性(只有'backgroundColor')。那么,这有可能吗?

提前致谢

【问题讨论】:

    标签: c# asp.net css gridview


    【解决方案1】:

    试试这个:

    在后面代码中的 RowDataBound 事件中,您可以获取您需要的单元格的 System.Web.UI.WebControls.TableCell 对象,并使用 CssClass 属性设置其样式:

    e.Row.Cells[0].CssClass = "myStyle"
    

    【讨论】:

    • [] 括号代替 () e.Row.Cells[0].CssClass = "className";
    • 现在我们有 VB 和 C# 解决方案。谢谢你告诉我。
    【解决方案2】:

    如果您想在网格视图上按每个单元格而不是行来添加它

    ItemStyle-CssClass="class name here"
    

    【讨论】:

    • 刚刚意识到这是绑定数据而不是.net
    【解决方案3】:

    您可以在 GridView 上设置 RowStyle-CssClass 属性,然后将样式应用于您拥有 CSS 样式的单元格(外部样式表、头部等...)。

    【讨论】:

      【解决方案4】:

      你可以试试

      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if(e.Row.RowType == DataControlRowType.DataRow)
          {
      
            if(condition)//Replace with your condition
            {
              e.Row.Cells[5].Attributes.Add("Style", "background: url(../Images/test.png) no-repeat 5px center
            }
          }
      }
      

      【讨论】:

      • 我很乐意帮助你 J_Ocampo
      猜你喜欢
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多