GridView列标题文本
本文关键字:文本 标题 GridView | 更新日期: 2023-09-27 18:21:23
我有这个GridView:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AllowPaging="True" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
这就是SqlDataSource1
:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connectionString %>"
ProviderName="<%$ ConnectionStrings:connectionString.ProviderName %>"
SelectCommand="SELECT count(city),city FROM users GROUP by city ORDER BY count(city) DESC;" ></asp:SqlDataSource>
在GridView
中,列的标题是count(city),city
。
我应该如何更改列名?
将SQL更改为:
SelectCommand = "SELECT count(city) As CityCount, city FROM users ..."
因此,使用ALIAS 将标头重命名为CityCount
为了获得更大的灵活性,请禁用从Gridview
自动生成列,并手动指定:AutoGenerateColumns="False"
<asp:GridView AutoGenerateColumns="False" ID="GridView1" runat="server"
DataSourceID="SqlDataSource1" AllowPaging="True" PageSize="20"
CellPadding="4" ForeColor="#333333" GridLines="None">
<asp:BoundField DataField="CityCount" HeaderText="Number of Cities" />
</asp:GridView>
您可以更改Sqlselect query
,在他的回答中使用别名作为编码业务提及
或
代码:动态设置头文本
GridView1.Columns[ColumnIndex].HeaderText = "Header text";
尝试这个
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="True"
PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"><%--add attribute AutoGenerateColumns="false" --%>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<Columns>
<asp:BoundField HeaderText="Total" DataField="City_Total" />
<asp:BoundField HeaderText="City" DataField="City" /> <%--Header Text => Whatever you want to display as name ;DataField=> name of field--%>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionString %>"
ProviderName="<%$ ConnectionStrings:connectionString.ProviderName %>" SelectCommand="SELECT count(city) as City_Total,city FROM users GROUP by city ORDER BY count(city) DESC;">
</asp:SqlDataSource>
将sql查询更改为以下
SELECT count(city) As CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;"
然后在网格视图中添加绑定字段,并将标题文本设置为您想要的
<Columns>
<asp:BoundField DataField="CityCount" HeaderText="City Count"
SortExpression="CityCount" />
<asp:BoundField DataField="city" HeaderText="City "
SortExpression="city" />
</Columns>
您应该像这样更改查询
SELECT count(city) AS CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;
您可以更改SQL
CMD="SELECT Count(City) As Count_City, etc,... FROM [Tbl_Name]
在我看来,你可以用两种方式:
1) 更新您的SQL查询:
SELECT count(city) as CityCount,city as CityName FROM users GROUP by city ORDER BY count(city) DESC;
2) 使用gridview的"TemplateField"或BoundField
功能(然后定义自己的列):
<asp:TemplateField HeaderText="Discount" HeaderText="City Count" >
<ItemTemplate>
<asp:Literal ID="CityCount" runat="server" Text='<%# Eval("CityCount").ToString() %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
在这种情况下,不要忘记将以下属性添加到网格视图中,否则您将有重复的列…:
AutoGenerateColumns="false"