我有这个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
。
我应该如何更改列名?
答案 0 :(得分:2)
将您的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>
答案 1 :(得分:2)
您可以使用别名更改您的 Sql select query
,并在其答案中提及
OR
代码:动态设置headertext
GridView1.Columns[ColumnIndex].HeaderText = "Header text";
答案 2 :(得分: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>
答案 3 :(得分: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>
答案 4 :(得分:0)
您应该像这样更改
SELECT count(city) AS CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;
答案 5 :(得分:0)
您可以更改您的SQL
CMD="SELECT Count(City) As Count_City, etc,... FROM [Tbl_Name]
答案 6 :(得分:0)
从我看到的地方,你可以做两种方式:
1)更新您的SQL查询:
SELECT count(city) as CityCount,city as CityName FROM users GROUP by city ORDER BY count(city) DESC;
2)使用gridview的'TEMlateField'或BoundField
功能(然后自己定义列):
<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"