Solved

Sorting GridView (datasource = custom collection)

Posted on 2011-02-15
1
525 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:webressurs
1 Comment
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 500 total points
ID: 34895233
Hi webressurs,

Below article help you to implement sorting with custom collection in gridview.

http://www.codeproject.com/KB/webforms/Strongly_typed_collection.aspx

Thanks,
netswap
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question