Question

I was using this file as a reference http://code.msdn.microsoft.com/VBASPNETGridView-19039173 I would like to follow the sorting function in that demo. But when I build a simple page . my gridview header is not a hyperlink

I already have sortExpression. but the header stays as regular text (not clickable).

This is the ASPX of my TEST aspx.

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplicationSandBox.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" SortExpression="PersonID" />
                <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

This is the ASPX file from the demo file. Could you please help me point out which properties makes the gridview header a hyperlink (clickable)? Thank you

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="DataInMemory.aspx.vb" Inherits="VBASPNETGridView.DataInMemory" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvPerson" runat="server" AutoGenerateColumns="False" BackColor="White" 
        BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            onpageindexchanging="gvPerson_PageIndexChanging" 
            onrowcancelingedit="gvPerson_RowCancelingEdit" 
            onrowdatabound="gvPerson_RowDataBound" onrowdeleting="gvPerson_RowDeleting" 
            onrowediting="gvPerson_RowEditing" onrowupdating="gvPerson_RowUpdating" 
            onsorting="gvPerson_Sorting">
        <RowStyle BackColor="White" ForeColor="#003399" />
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
                <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" 
                    SortExpression="PersonID" />
                <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        </asp:GridView>

        <br />
        <asp:LinkButton ID="lbtnAdd" runat="server" onclick="lbtnAdd_Click">AddNew</asp:LinkButton>
        <br />
        <br />
        <asp:Panel ID="pnlAdd" runat="server" Visible="False">
            Last name:
            <asp:TextBox ID="tbLastName" runat="server"></asp:TextBox>
            <br />
            <br />
            First name:
            <asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:LinkButton ID="lbtnSubmit" runat="server" onclick="lbtnSubmit_Click">Submit</asp:LinkButton>
            &nbsp;&nbsp;&nbsp;
            <asp:LinkButton ID="lbtnCancel" runat="server" onclick="lbtnCancel_Click">Cancel</asp:LinkButton>

        </asp:Panel>
    </div>
    </form>

</body>
</html>
Was it helpful?

Solution

When i use this code it displays the Header as links - is that what your after?

<asp:GridView ID="GridView1" runat="server" OnSorting="GridView1_Sorting" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" AllowSorting="true">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
            <asp:BoundField DataField="PersonID" HeaderText="PersonID" ReadOnly="False" SortExpression="PersonID" />
            <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

OTHER TIPS

Gridview is made sortable with the AllowSorting property:

    <asp:GridView ID="GridView1" 
        runat="server" 
        -->AllowPaging="True" <--
        AllowSorting="True" >
        <Columns>
            {insert your columns}
        </Columns>
    </asp:GridView>

That will make your headers clickable. The sample page didn't set AllowSorting on the control. The code later in the page shows where they set this in code-behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
' The Page is accessed for the first time. 
If Not IsPostBack Then 
    ' Enable the GridView paging option and  
    ' specify the page size. 
    gvPerson.AllowPaging = True 
    gvPerson.PageSize = 15 


    ' Enable the GridView sorting option. 
-->        gvPerson.AllowSorting = True <--


    ' Initialize the sorting expression. 
    ViewState("SortExpression") = "PersonID ASC" 


    ' Populate the GridView. 
    BindGridView() 
End If 
End Sub 

Hope that helps. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top