Solved

Sorting GridView (datasource = custom collection)

Posted on 2011-02-15
1
520 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now