20

I have a gridview and enabled sorting. When running the application I click on the first column to sort. And I get this error: "The GridView 'gvOutlookMeldingen' fired event Sorting which wasn't handled."

This is the gridview:

<asp:GridView ID="gvOutlookMeldingen" runat="server" AllowSorting="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" onselectedindexchanged="GridView_SelectedIndexChanged">
    <Columns>
        <asp:TemplateField HeaderText="Melder" ItemStyle-HorizontalAlign="Center" SortExpression="Melder">
            <HeaderStyle BorderColor="#1A3491" Width="130px"></HeaderStyle>
            <ItemStyle Height="20px" HorizontalAlign="Center"></ItemStyle>
            <ItemTemplate>
                <%# (string)Eval("Melder") %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Onderwerp" HeaderText="Onderwerp" />
        <asp:TemplateField HeaderText="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" />
        <asp:BoundField DataField="OutlookID" HeaderText="OutlookID" />
    </Columns>
</asp:GridView>

Any help is appreciated

Tassisto
  • 9,877
  • 28
  • 100
  • 157
  • as stated below i can suggest looking into Linq to SQL. It seems like this table is populated using sql. With Linq to SQL it makes this a little easier to sort things – Neale Mar 18 '11 at 19:34
  • A better way is http://stackoverflow.com/questions/3966835/sorting-gridview – Sami Mar 18 '13 at 16:12

4 Answers4

24

You are missing SortExpression's in your BoundField's as mentioned in the other answers.

You are also using a TemplateField which, depending on what is generating your data, may require manual sorting beyond use of SortExpression.

If this is the case, then to sort manually, one method is to add an OnSorting callback to the GridView, SortExpression's to your fields and a method to callback in your code-behind.

This would result in markup and code similar to (untested):

<asp:GridView ID="gvOutlookMeldingen" runat="server" 
    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:

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.
    }
}
Tassisto
  • 9,877
  • 28
  • 100
  • 157
Kynth
  • 2,597
  • 1
  • 18
  • 24
  • what about the datasource I use a datatable. so I have to sort that datatable too? do you know how? – Tassisto Mar 18 '11 at 13:28
  • There's a few ways to do it, I usually use Linq out of habit. There's a good SO answer for that here: http://stackoverflow.com/questions/10855/linq-query-on-a-datatable – Kynth Mar 18 '11 at 13:36
  • What if I am using files from a folder for DataSource, how would I be able to achieve that? http://stackoverflow.com/questions/24491778/how-to-allow-sorting-in-gridview – SearchForKnowledge Jun 30 '14 at 14:03
9

This code might help (for you guys googling this old post):

protected void gvOutlookMeldingen_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = (DataTable)Session["mySessionStoredTable"];
    dt.DefaultView.Sort = e.SortExpression // column name
        + " " + SortDir(e.SortExpression); // sort direction
    gv.DataSource = dt;
    gv.DataBind();
}

private string SortDir(string sColumn)
{
    string sDir = "asc"; // ascending by default
    string sPreviousColumnSorted = ViewState["SortColumn"] != null 
        ? ViewState["SortColumn"].ToString() 
        : "";

    if (sPreviousColumnSorted == sColumn) // same column clicked? revert sort direction
        sDir = ViewState["SortDir"].ToString() == "asc" 
            ? "desc"
            : "asc";
    else
        ViewState["SortColumn"] = sColumn; // store current column clicked

    ViewState["SortDir"] = sDir; // store current direction

    return sDir;
}
Turbo
  • 101
  • 1
  • 6
5

You need to set the SortExpression attribute of the columns you want to sort on. And you need to add an event handler to the Sorting event to make the sort work on your DataSource.

For more information, see this MSDN article, or this example with sorting and paging.

Prutswonder
  • 9,894
  • 3
  • 27
  • 39
3

I assume the datasource of your grid view is a DataTable so I think you have to add

SortExpression="column name"

in every asp:Boundfield that you like to be able to sort,

for example

<asp:BoundField DataField="Melder" SortExpression="Melder" HeaderText="Melder" />
bAN
  • 13,375
  • 16
  • 60
  • 93
  • I have updated the question, I did what you said but I still get the error. --> "The GridView 'gvOutlookMeldingen' fired event Sorting which wasn't handled." – Tassisto Mar 18 '11 at 13:09
  • What is the datasource of the gridview is it a generic list<>, a datatable? – bAN Mar 18 '11 at 13:10
  • it's a datatable and i have another gridview which I fill with the help of SQL – Tassisto Mar 18 '11 at 13:13