简体   繁体   English

如何允许排序gridview?

[英]How to allow sorting of a gridview?

I have a gridview and enabled sorting. 我有一个gridview并启用了排序。 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." 我得到这个错误:“GridView'gvOutlookMeldingen'触发了事件排序,但没有处理。”

This is the gridview: 这是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 任何帮助表示赞赏

You are missing SortExpression 's in your BoundField 's as mentioned in the other answers. 你在其他答案中提到的BoundField中缺少SortExpression

You are also using a TemplateField which, depending on what is generating your data, may require manual sorting beyond use of SortExpression . 您还使用了TemplateField ,根据生成数据的内容,可能需要手动排序SortExpression使用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. 如果是这种情况,那么要手动排序,一种方法是将一个OnSorting回调添加到GridView ,将SortExpression添加到您的字段以及一个在您的代码隐藏中回调的方法。

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

This code might help (for you guys googling this old post): 这段代码可能会有所帮助(对于你们这个老帖子的google):

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

You need to set the SortExpression attribute of the columns you want to sort on. 您需要设置要排序的列的SortExpression属性。 And you need to add an event handler to the Sorting event to make the sort work on your DataSource. 并且您需要向Sorting事件添加事件处理程序,以使排序在DataSource上运行。

For more information, see this MSDN article , or this example with sorting and paging . 有关详细信息,请参阅此MSDN文章带有排序和分页的此示例

I assume the datasource of your grid view is a DataTable so I think you have to add 我假设你的网格视图的数据源是一个DataTable,所以我认为你必须添加

SortExpression="column name"

in every asp:Boundfield that you like to be able to sort, 在每个asp:您希望能够排序的Boundfield,

for example 例如

<asp:BoundField DataField="Melder" SortExpression="Melder" HeaderText="Melder" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM