如何让一个GridView的排序? [英] How to allow sorting of a gridview?
问题描述
我有一个GridView并启用排序。运行应用程序时我点击第一列进行排序。而我得到这个错误:GridView的'gvOutlookMeldingen解雇事件排序这是不处理的。
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."
这是在GridView:
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>
任何帮助是pciated AP $ P $
Any help is appreciated
推荐答案
您缺少 SORTEX pression的在你的
的BoundField <
/ code>的在其他的答案提到。
You are missing SortExpression
's in your BoundField
's as mentioned in the other answers.
您还使用模板列
其中,取决于是什么在生成你的数据,可能需要手工分拣超出使用 SORTEX pression的
。
You are also using a TemplateField
which, depending on what is generating your data, may require manual sorting beyond use of SortExpression
.
如果是这种情况,那么手动排序,一种方法是增加一个 OnSorting
回调至 GridView控件
, SORTEX pression
的你的领域和方法,以在code-背后回调。
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.
这将导致标记和code相似(未经测试):
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>
...和:
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.
}
}
这篇关于如何让一个GridView的排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!