0

我有这个 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>

这个ID是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.

我应该如何更改列名?

4

7 回答 7

2

将您的 SQL 更改为:

SelectCommand = "SELECT count(city) As CityCount, city FROM users ..."

因此将标头重命名为CityCount使用 ALIAS

为了获得更大的灵活性,请禁用从 中自动生成列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>
于 2013-07-05T12:00:55.087 回答
2

您可以在他的回答中使用别名作为codingbiz来更改您的Sql select query

或者

代码: 动态设置headertext

GridView1.Columns[ColumnIndex].HeaderText = "Header text";
于 2013-07-05T12:02:27.043 回答
0

从我看到的地方,您可以通过两种方式做到这一点:

1) 更新您的 SQL 查询:

SELECT count(city) as CityCount,city as CityName FROM users GROUP by city ORDER BY count(city) DESC;

2)使用'TemplateField'或BoundFieldgridview的功能(然后定义你自己的列):

<asp:TemplateField HeaderText="Discount" HeaderText="City Count" >
    <ItemTemplate>   
        <asp:Literal ID="CityCount" runat="server" Text='<%# Eval("CityCount").ToString() %>'></asp:Literal>
  </ItemTemplate>                     
</asp:TemplateField> 

在这种情况下,不要忘记将以下属性添加到您的 gridview 中,否则您将有重复的列...:

AutoGenerateColumns="false"
于 2013-07-05T12:07:37.903 回答
0

尝试这个

    <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>
于 2013-07-05T12:02:58.430 回答
0

将您的 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>
于 2013-07-05T12:04:05.617 回答
0

您应该像这样更改查询

SELECT count(city) AS CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;
于 2013-07-05T12:04:17.970 回答
0

你可以改变你的 SQL

  CMD="SELECT Count(City) As Count_City, etc,... FROM [Tbl_Name]
于 2013-07-05T12:05:25.900 回答