Internal gridlines in GridView in ASP.NET

2019-02-27 08:56发布

I have a GridView in ASP.NET 2.0 that I want to have show only internal gridlines. Here is my markup and CSS so far:

<asp:GridView ID="myGrid" runat="server" GridLines="None" CssClass="myDataGridClass">
    <Columns>
        ...columns here...
    </Columns>
</asp:GridView>

CSS:

.myDataGridClass>tbody>tr>td /* Apply border to all cells */
{
   border:1px solid black;
}

.myDataGridClass>tbody>tr>th /* Apply border to headers */
{
   border:1px solid black;
}

.myDataGridClass>tbody>tr>td:last-child /* Remove right-side border */
{
   border-right-width:0;
}

.myDataGridClass>tbody>tr>td:first-child /* Remove left-side border */
{
   border-left-width:0;
}

.myDataGridClass>tbody>tr>th:last-child /* Remove right-side header border */
{
   border-right-width:0;
}

.myDataGridClass>tbody>tr>th:first-child /* Remove left-side header border */
{
   border-left-width:0;
}

.myDataGridClass>tbody>tr:last-child>td /* Remove bottom border */
{
    border-bottom-width:0;
}

.myDataGridClass>tbody>tr>th /* Remove top border */
{
   border-top-width:0;
}

Am I right in thinking that there must be an easier way to do this? My method above already does not work in IE, since I'm using last-child.

2条回答
走好不送
2楼-- · 2019-02-27 09:31
protected void Page_Load(object sender, EventArgs e)
{
    this.myGrid.Attributes.Add("bordercolor", "#000");
}

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.

See comments under this blog post:

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

查看更多
可以哭但决不认输i
3楼-- · 2019-02-27 09:35
GridLines="None" CellSpacing="2" BackColor="White"
查看更多
登录 后发表回答