排序gridview - 找不到列

时间:2013-09-28 00:41:10

标签: asp.net sorting gridview

相对新手在这里。发现我必须编写一个事件处理程序来处理来自objectdatasource的数据排序。找到了很多代码的引用,如下所示,但是当代码运行时,我收到消息“找不到列xxx ”,其中xxx是我要排序的列的名称。

我已经验证了e.SortExpression正在返回正确的列名,但问题似乎在下面的第二行,我尝试从GridView1.Datasource创建一个数据表(dt)。此语句后面的dt值为“nothing”,因此其余代码都会失败。

此外,我列出了大部分gridview定义,以防有用。

Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting
    Dim dt As Data.DataTable = New Data.DataTable(GridView1.DataSource)
    If Not dt Is Nothing Then
        dt.DefaultView.Sort = e.SortExpression
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub

ASPX代码段:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="ObjectDataSource1" CellPadding="4" 
        EmptyDataText="No records found for your search" ForeColor="#333333" 
        GridLines="None" Width="930px" BorderColor="#CCCCCC" Font-Size="12px" AllowSorting="True">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="CustName" HeaderText="CustName" 
                SortExpression="CustName" />
            <asp:BoundField DataField="CustType" HeaderText="CustType" 
                SortExpression="CustType" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" />
            <asp:TemplateField HeaderText="Address" SortExpression="Addr1">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Addr1") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            <asp:BoundField DataField="ContactName" HeaderText="ContactName" 
                SortExpression="ContactName" />
            <asp:BoundField DataField="LastContactDate" HeaderText="LastContact" SortExpression="LastContactDate" dataformatstring="{0:d}" />
            <asp:BoundField DataField="CustSince" HeaderText="CustSince" 
                SortExpression="CustSince" dataformatstring="{0:MM/dd/yyyy}" />
            <asp:BoundField DataField="Status" HeaderText="Status"  
                SortExpression="Status" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
            <asp:BoundField DataField="Source" HeaderText="Source" 
                SortExpression="Source" />
            <asp:BoundField DataField="RelMgr" HeaderText="RelMgr" 
                SortExpression="RelMgr" />
            <asp:TemplateField HeaderText="RelMgrInfo">
                 <ItemTemplate>
                     <asp:HyperLink ID="RelMgrEmailLink" runat="server" NavigateUrl='<%#Eval("RelMgrEmail", "mailto:{0}")%>' Text='<%# Eval("RelMgrEmail") + "<br>" + Eval("RelMgrPhone")%>  '> </asp:HyperLink>
                 </ItemTemplate>
             </asp:TemplateField>
        </Columns>

谢谢!

1 个答案:

答案 0 :(得分:0)

如果使用ObjectDataSource,则不必处理排序事件。相反,将SortParameterName设置为字符串(如“OrderBy”)并扩充ObjectDataSource用于使用此参数检索数据的业务方法签名:

public IEnumerable<Whatever> Retrieve( string OrderBy, ... other arguments ) ...

网格会将参数值传递给ODS,然后ODS会将其传递给您的方法。