YosiFZ
YosiFZ

Reputation: 7890

GridView columns header text

I Have this 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>

And this id the 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>

And in the GridView the headers for the columns is count(city),city.

How should i change the columns names?

Upvotes: 0

Views: 3962

Answers (7)

codingbiz
codingbiz

Reputation: 26376

Change your SQL to:

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

Therefore renaming the header to CityCount using ALIAS

For more flexibility, disable autogeneration of column from the Gridview and specify it manually: 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>

Upvotes: 2

Satinder singh
Satinder singh

Reputation: 10198

You can change your Sql select query using alias as codingbiz mention in his answer

OR

Code: Dynamically set headertext

GridView1.Columns[ColumnIndex].HeaderText = "Header text";

Upvotes: 2

Laurent S.
Laurent S.

Reputation: 6946

From where I see it, You could do it 2 ways :

1) Update your SQL query :

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

2) Use the ´TemplateField´ or BoundField functionality of gridview (and then defining yourself your columns) :

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

In that case don't forget to add the following attribute to your gridview, cause else you'll have duplicate column... :

AutoGenerateColumns="false"

Upvotes: 0

Shujaat Abdi
Shujaat Abdi

Reputation: 566

You Can Change Your SQL

  CMD="SELECT Count(City) As Count_City, etc,... FROM [Tbl_Name]

Upvotes: 0

Abbas Amiri
Abbas Amiri

Reputation: 3204

You should change the query like this

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

Upvotes: 0

Kevin Kunderman
Kevin Kunderman

Reputation: 2134

Change your sql query to the following

SELECT count(city) As CityCount, city FROM users GROUP by city ORDER BY count(city) DESC;"

and then within the grid view add bound fields with the header text set to whatever you want

<Columns>
    <asp:BoundField DataField="CityCount" HeaderText="City Count" 
                                                SortExpression="CityCount" /> 
    <asp:BoundField DataField="city" HeaderText="City " 
                                                SortExpression="city" /> 
</Columns>

Upvotes: 0

sangram parmar
sangram parmar

Reputation: 8726

try this

    <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>

Upvotes: 0

Related Questions