Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c# - How to allow sorting of a gridview?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...