【问题标题】:How to set border to ItemTemplates in GridView如何在 GridView 中为 ItemTemplates 设置边框
【发布时间】:2012-04-18 04:58:53
【问题描述】:

如何在 GridView 中为 ItemTemplates 设置边框?

以下是 Gridview 代码。

<div>
   <asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns = "false" Font-Names = "Arial" 
    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B"  
    HeaderStyle-BackColor = "green" AllowPaging ="true"   
    OnPageIndexChanging = "OnPaging" OnRowDataBound = "RowDataBound"
    PageSize = "10" >
   <Columns>
    <asp:TemplateField>
        <HeaderTemplate>
            <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" />
        </HeaderTemplate> 
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox1" runat="server" onclick = "Check_Click(this)"/>
        </ItemTemplate>
    </asp:TemplateField> 
    <asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID" HeaderText = "CustomerID" />
    <asp:BoundField ItemStyle-Width = "150px" DataField = "City" HeaderText = "City"/>
    <asp:BoundField ItemStyle-Width = "150px" DataField = "Country" HeaderText = "Country"/>
    <asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode" HeaderText = "PostalCode"/>
   </Columns> 
   <AlternatingRowStyle BackColor="#C2D69B"  />
</asp:GridView> 

所有有界字段都有边框,但没有 ItemTemplated 字段。

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    一种解决方法是通过点击 GridView 的 RowDataBound 事件来做到这一点:

    protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         foreach (TableCell tc in e.Row.Cells)
         {
             tc.Attributes["style"] = "border-color: #c3cecc";
         }
    }
    

    更多信息在这里:http://codersbarn.com/post/2009/05/31/Set-Color-of-GridLines-in-Gridview.aspx

    查看 cmets 部分以获得更好的方法...

    protected void Page_Load(object sender, EventArgs e)
    {
       this.GridView1.Attributes.Add("bordercolor", "c3cecc");
    }
    

    “使用 GridView,声明性边框颜色属性添加了一个内联样式声明,该声明仅适用于表格本身,而不适用于单个单元格。

    以编程方式添加边框颜色属性不使用内联样式,而是使用 HTML 边框颜色属性,浏览器将其应用于表格内的所有边框。"

    还有一件事,如果您使用 Eric Meyer 的重置,它会破坏 GridView 中的表格呈现。该特定问题的解决方案是从重置规则中删除所有表格元素。

    【讨论】:

      【解决方案2】:

      您需要使用ItemStyle 字段。在TemplateField 文档中找到。

      【讨论】:

        猜你喜欢
        • 2012-08-16
        • 1970-01-01
        • 2013-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多