为 asp.net gridview 中的 header 文本设置样式
Styling header text in asp.net gridview
Q1。我在网格视图中有一个超链接字段,如下所示:
<asp:HyperLinkField Text="Analyze" />
我希望它始终带有下划线,而不仅仅是在我悬停时。我该怎么做?
Q2。我希望 gridView 的 header 文本居中对齐。
我试过了:HeaderStyle-HorizontalAlign="Left"
但是没用。
我也试过用 :
制作 css class
<style type="text/css">
.header-center{
text-align:center;}
</style>
但这也行不通。
我基本上是在尝试将 header 文本居中,没什么特别的,但它并没有发生。
您必须将 BodyContent 中的代码替换为以下代码。
注意:您可以将自己的颜色添加到网格中
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<style type="text/css">
#MainContent_GridView2 tbody tr th {
text-align: center;
background: #808080;
height: 40px;
}
#MainContent_GridView2 tbody tr {
height: 20px;
background-color: #CCC;
}
#MainContent_GridView2 tbody tr:hover {
background-color: #808080;
}
#MainContent_GridView2 tbody tr td {
text-align: center;
height: 30px;
}
.linkfield {
text-decoration: underline;
text-align: center;
}
</style>
<asp:GridView ID="GridView2"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
AllowSorting="True"
PageSize="25"
Height="800px"
Width="1200px"
OnPageIndexChanging="GridView2_PageIndexChanging">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="runId" HeaderText="RunID" DataNavigateUrlFormatString="RunAnalysis.aspx?runId={0}" ItemStyle-Width="10%">
<ItemStyle Width="10%" CssClass="linkfield"></ItemStyle>
</asp:HyperLinkField>
<asp:BoundField DataField="prodDate" HeaderText="Date" DataFormatString="{0:MM/dd/yy}" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="buildNumber" HeaderText="Build Number" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="PercentAnalysed" DataTextField="PercentAnalysed" HeaderText="Percent Analysed" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:HyperLinkField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
</asp:GridView>
</asp:Content>
希望对您有所帮助。
Q1。我在网格视图中有一个超链接字段,如下所示:
<asp:HyperLinkField Text="Analyze" />
我希望它始终带有下划线,而不仅仅是在我悬停时。我该怎么做?
Q2。我希望 gridView 的 header 文本居中对齐。
我试过了:HeaderStyle-HorizontalAlign="Left"
但是没用。
我也试过用 :
制作 css class<style type="text/css">
.header-center{
text-align:center;}
</style>
但这也行不通。
我基本上是在尝试将 header 文本居中,没什么特别的,但它并没有发生。
您必须将 BodyContent 中的代码替换为以下代码。
注意:您可以将自己的颜色添加到网格中
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<style type="text/css">
#MainContent_GridView2 tbody tr th {
text-align: center;
background: #808080;
height: 40px;
}
#MainContent_GridView2 tbody tr {
height: 20px;
background-color: #CCC;
}
#MainContent_GridView2 tbody tr:hover {
background-color: #808080;
}
#MainContent_GridView2 tbody tr td {
text-align: center;
height: 30px;
}
.linkfield {
text-decoration: underline;
text-align: center;
}
</style>
<asp:GridView ID="GridView2"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
AllowSorting="True"
PageSize="25"
Height="800px"
Width="1200px"
OnPageIndexChanging="GridView2_PageIndexChanging">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="runId" DataTextField="runId" HeaderText="RunID" DataNavigateUrlFormatString="RunAnalysis.aspx?runId={0}" ItemStyle-Width="10%">
<ItemStyle Width="10%" CssClass="linkfield"></ItemStyle>
</asp:HyperLinkField>
<asp:BoundField DataField="prodDate" HeaderText="Date" DataFormatString="{0:MM/dd/yy}" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="buildNumber" HeaderText="Build Number" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:BoundField>
<asp:HyperLinkField DataNavigateUrlFields="PercentAnalysed" DataTextField="PercentAnalysed" HeaderText="Percent Analysed" ItemStyle-Width="10%">
<ItemStyle Width="10%"></ItemStyle>
</asp:HyperLinkField>
</Columns>
<PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" />
</asp:GridView>
</asp:Content>
希望对您有所帮助。