Link to home
Start Free TrialLog in
Avatar of webressurs
webressursFlag for Norway

asked on

Sorting GridView (datasource = custom collection)

I have a GridView binded to a custom collection. How can I implement GridView sorting?

The gridview displays an user log. I need to sort the gridview by clicking the gridview headings. For example: When clicking the "Attempts" header I need to sort the gridview by "Attempts" (Asc/Desc).

Please see attached code. Thanks for all help!

<asp:GridView runat="server" ID="grdLog" AutoGenerateColumns="false" OnPageIndexChanging="grdLog_PageIndexChanging" AllowSorting="true" AllowPaging="true" PageSize="50">
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Id") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="User">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "User") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Attempts" SortExpression="Attempts">
            <ItemTemplate>
                <%# DataBinder.Eval(Container.DataItem, "Attempts") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>



protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        BindLog();
    }
}


protected void BindLog()
{
    LogCollection Log = LogHandler.GetLog();
    this.grdLog.DataSource = Log;
    this.grdLog.DataBind();
}


protected void grdLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    this.grdLog.PageIndex = e.NewPageIndex;
    BindLog();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Swapnil
Swapnil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial