Click here to Skip to main content
15,889,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I would like to display the gridview by sorting the column 'mem_id'. Hereunder is my code:

XML
<asp:GridView ID="GridView2" runat="server"  HorizontalAlign="Center"  AutoGenerateColumns="False"   AllowSorting="true"
            PageSize="12" onpageindexchanging="GridView2_PageIndexChanging" style="border-color: #808000; font-size:small; top: 176px; left: 27px; position: absolute; height: 75px; width:1350px" >
            <RowStyle HorizontalAlign="Center" />
            <Columns >
            <asp:BoundField DataField="mem_id" HeaderText="Mem_ID"/>
            <asp:BoundField DataField="mem_name" HeaderText="Mem Name"/>
            <asp:BoundField DataField="mobile" HeaderText="Cell No:"/>
            </Columns>
            </asp:GridView>



Can anyone guide me how to sort on mem_id column.
Regards.
Posted

1 solution

You are missing SortExpression's in your BoundField's
here i put the sample code for the sample
<asp:gridview id="gvOutlookMeldingen" runat="server" xmlns:asp="#unknown">
        AllowSorting="True" 
        OnSorting="gvOutlookMeldingen_Sorting"
        AutoGenerateColumns="False" 
        AutoGenerateSelectButton="True" 
        onselectedindexchanged="GridView_SelectedIndexChanged">
        <columns>
            <asp:boundfield datafield="Melder" headertext="Melder" sortexpression="Melder" />
            <asp:boundfield datafield="Onderwerp" headertext="Onderwerp" sortexpression="Onderwerp" />
            <asp:templatefield headertext="Omschrijving" sortexpression="Omschrijving">
                <itemtemplate>
                    <div style="overflow:auto; width: 500px; height: 200px;">
                        <asp:label id="lblOmschrijving" runat="server" text="<%# Bind("Omschrijving")%>"></asp:label>
                    </div>
                </itemtemplate>
            </asp:templatefield>
            <asp:boundfield datafield="Meldingsdatum" headertext="Meldingsdatum" sortexpression="Meldingsdatum" />
            <asp:boundfield datafield="OutlookID" headertext="OutlookID" sortexpression="OutlookID" />
        </columns>
    </asp:gridview>

And from to the code behing put this...
C#
protected void gvOutlookMeldingen_Sorting(object sender, GridViewSortEventArgs e)
        {
            switch (e.SortExpression)
            {
                case "Melder":
                    if (e.SortDirection == SortDirection.Ascending)
                    {
                        gvOutlookMeldingen.DataSource = // Asc query for Melder field;
                        gvOutlookMeldingen.DataBind();
                    }
                    else
                    {
                        gvOutlookMeldingen.DataSource = // Desc query for Melder field ;
                        gvOutlookMeldingen.DataBind();
                    }

                    break;

                // case statements for your other fields.
            }
        }
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 11-Jul-13 5:51am    
I have already bound the Grideview. Where should I add that Asc query?
GridView2.DataSource = ds;
GridView2.DataBind();
Can you give code for that //Asc query for Melder field

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900