How to set border to ItemTemplates in GridView

十年热恋 提交于 2020-01-14 05:05:48

问题


How to set border for ItemTemplates in GridView?

Following is the Gridview code.

<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> 

All Bounded Fields are getting borders but not the ItemTemplated Fields.


回答1:


A workaround is to do this by tapping into the GridView's RowDataBound event:

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

More info here: http://codersbarn.com/post/2009/05/31/Set-Color-of-GridLines-in-Gridview.aspx

See comments section for a better way...

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

"With the GridView, the declarative bordercolor attribute adds an inline style declaration which only applies to the table itself, not individual cells.

Adding the bordercolor attribute programmatically does not use an inline style, but uses the HTML bordercolor property, which browsers apply to ALL borders inside the table."

One more thing, if you are using Eric Meyer's reset, it breaks the table rendering in the GridView. Solution to that particular issue is to remove all table elements from the reset rule.




回答2:


You need to use the ItemStyle field. Found in the TemplateField documentation.



来源:https://stackoverflow.com/questions/10203046/how-to-set-border-to-itemtemplates-in-gridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!