I'm using CSS to style a GridView. I have everything working fine except for the padding in the cells that contain the data. I've googled and found multiple solutions that involve another style on the Item when using Bound fields. However, I am not using bound fields. I am binding to a List(Of MyObject). How would I add padding around the data in the cells?
One almost-solution was to style the TR and TD elements. This works fine ... until I add paging to the GridView. The padding also applies to the page counters, which I do not want. This is because the paging row is just another TR in the rendered HTML table.
Here's a little of what I have going on:
.aspx Page:
<asp:GridView ID="gvTerritories"
runat="server"
CssClass="gridview"
AutoGenerateSelectButton="True"
GridLines="None"
AllowPaging="true"
PageSize="10">
<HeaderStyle CssClass="gridViewHeader" />
<RowStyle CssClass="gridViewRow" />
<AlternatingRowStyle CssClass="gridViewAltRow" />
<SelectedRowStyle CssClass="gridViewSelectedRow" />
<PagerStyle CssClass="gridViewPager" />
</asp:GridView>
CSS:
.gridview {
font-family: Arial;
background-color: #FFFFFF;
border: solid 1px #CCCCCC;
}
.gridViewHeader {
background-color: #0066CC;
color: #FFFFFF;
padding: 4px 50px 4px 4px;
text-align: left;
border-width: 0px;
border-collapse: collaps开发者_JS百科e;
}
.gridViewRow {
background-color: #99CCFF;
color: #000000;
border-width: 0px;
border-collapse: collapse;
}
.gridViewAltRow {
background-color: #FFFFFF;
border-width: 0px;
border-collapse: collapse;
}
.gridViewSelectedRow {
background-color: #FFCC00;
color: #666666;
border-width: 0px;
border-collapse: collapse;
}
.gridViewPager
{
background-color: #0066CC;
color: #FFFFFF;
text-align: left;
padding: 0px;
}
The gridViewHeader
class doesn't apply the padding to the TH row. The gridViewPager
class doesn't apply the padding of 0px to the rendered HTML in the "pager" TR.
Ok, I figured it out. The key was to make sure to style .gridViewPager td
to override .gridview td
. Credit for the majority of this code goes to the selected correct answer in this SO posting. Here is the whole enchalada:
.aspx:
<asp:GridView ID="gvTerritories"
runat="server"
CssClass="gridview"
AutoGenerateSelectButton="True"
GridLines="None"
AllowPaging="true"
PageSize="10">
<HeaderStyle CssClass="gridViewHeader" />
<RowStyle CssClass="gridViewRow" />
<AlternatingRowStyle CssClass="gridViewAltRow" />
<SelectedRowStyle CssClass="gridViewSelectedRow" />
<PagerStyle CssClass="gridViewPager" />
</asp:GridView>
CSS:
.gridview {
font-family: Arial;
background-color: #FFFFFF;
border: solid 1px #CCCCCC;
}
.gridview td {
padding: 5px 50px 5px 5px;
}
.gridview th {
padding: 5px 50px 5px 5px;
text-align: left;
}
.gridview th a{
color: #003300;
text-decoration: none;
}
.gridview th a:hover{
color: #003300;
text-decoration: underline;
}
.gridview td a{
color: #003300;
text-decoration: none;
}
.gridview td a:hover {
color: red;
text-decoration:underline;
}
.gridViewHeader {
background-color: #0066CC;
color: #FFFFFF;
text-align: left;
}
.gridViewRow {
background-color: #99CCFF;
color: #000000;
}
.gridViewAltRow {
background-color: #FFFFFF;
}
.gridViewSelectedRow {
background-color: #FFCC00;
color: #666666;
}
/* Of course, this doesn't work with IE6. Works fine with Firefox, though. */
.gridview tr.gridViewRow:hover td, .gridview tr.gridViewAltRow:hover td {
background-color: #CCCCCC;
color: #000000;
}
.gridViewPager
{
background-color: #0066CC;
color: #FFFFFF;
text-align: left;
}
.gridViewPager td {
padding: 3px;
}
.gridViewPager td a {
color: #FFFFFF;
text-decoration: none;
}
.gridViewPager td a:hover {
color: #FFFFFF;
text-decoration: underline;
}
/* The currently selected page number is rendered by ASP.NET in a span tag in the td. */
.gridViewPager span {
color: #000000;
}
#divGridView {
margin-top: 1.5em;
}
You could add a style similar to this.
.gridview td {
padding: 2px;
}
or this for your header
.gridview th {
padding: 2px;
}
CSS can be applied as shown below,
<asp:BoundField DataField="SNo" HeaderText="S. No">
<ItemStyle CssClass="YourCSS" />
</asp:BoundField>
精彩评论