必须在使用DataView之前设置DataTable

时间:2014-04-10 16:16:50

标签: c# asp.net .net binding ado.net

您好我查看搜索结果“必须在使用DataView之前设置DataTable”。但他们的解决方案都没有解决我的问题或指出我正确的方向。

我在这里出错了,当我按下标题来整理gridview表时,我只是得到一个错误而没有排序。该错误与此帖子的标题相同。

编辑:不确定它是否相关,但我从MS SQL数据库填充页面加载的gridview

标记式

<asp:GridView ID="_propertyGridView" runat="server" CssClass="table table-hover table-striped" AutoGenerateColumns="false" AllowSorting="true" GridLines="None" OnRowCommand="PropertyRowCommand"  Width="100%">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Ref" SortExpression="Id" HeaderStyle-HorizontalAlign="Left"   />
        <asp:BoundField DataField="PostCode" HeaderText="Post Code" SortExpression="PostCode" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="ContractsFinishedOn" HeaderText="Contract Signed On" SortExpression="ContractsFinishedOn" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="Mobile" HeaderText="Mobile No." SortExpression="Mobile" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="LandLordEmail" HeaderText="Owners Email" SortExpression="LandLordEmail" HeaderStyle-HorizontalAlign="Left" />
        <asp:BoundField DataField="MoveInDate" HeaderText="Move In Date" SortExpression="MoveInDate" HeaderStyle-HorizontalAlign="Left" />
        <asp:TemplateField HeaderText="Status" SortExpression="Status1">
            <ItemTemplate>
                <asp:CheckBox ID="_tenantPaymentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("TenantReceipt") %>' />
                <asp:LinkButton ID="_tenantPaymentLink" Text="Send Tenant Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="TenantEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the tenant, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" SortExpression="Status2">
            <ItemTemplate>
                <asp:CheckBox ID="_landlordInfoCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordInfo") %>' />
                <asp:LinkButton ID="_landlordInfoLink" Text="Request Landlord Info" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordInfoEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Status" SortExpression="Status3">
            <ItemTemplate >
                <asp:CheckBox ID="_landlordRentCheckBox" runat="server" Enabled="false" Checked='<%#Bind("LandlordReceipt") %>' />
                <asp:LinkButton ID="_landlordRentLink" Text="Send Landlord Receipt" CommandArgument='<%#Bind("Id") %>' CommandName="LandlordEmail" runat="server" OnClientClick="javascript:return confirm('This will email &amp; sms the landlord, please make sure its correct');" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码隐藏

protected void _propertyGridView_Sorting(object sender, GridViewSortEventArgs e)
{
    try
    {
        string sortExpression = e.SortExpression;
        ViewState["z_sortexpresion"] = e.SortExpression;
        if (GridViewSortDirection == SortDirection.Ascending)
        {
            GridViewSortDirection = SortDirection.Descending;
            SortGridView(sortExpression, "DESC");
        }
        else
        {
            GridViewSortDirection = SortDirection.Ascending;
            SortGridView(sortExpression, "ASC");
        }
    }
    catch (Exception ex)
    {
        SearchErrorLbl.Text = "error in such";
    }
}

public SortDirection GridViewSortDirection
{
    get
    {
        if (ViewState["sortDirection"] == null)
            ViewState["sortDirection"] = SortDirection.Ascending;
        return (SortDirection)ViewState["sortDirection"];
    }
    set
    {
        ViewState["sortDirection"] = value;
    }

}

private void SortGridView(string sortExpression, string direction)
{
    DTSorting = new DataView(DTSorting, "", sortExpression + " " + direction, DataViewRowState.CurrentRows).ToTable();
    _propertyGridView.DataSource = DTSorting;
    _propertyGridView.DataBind();
}

public DataTable DTSorting
{
    get
    {
        if (ViewState["Sorting"] != null)
            return (DataTable)ViewState["Sorting"];
        else
            return null;
    }
    set
    {
        ViewState["Sorting"] = value;
    }
}

1 个答案:

答案 0 :(得分:0)

我觉得问题出在SortGridView方法之内。您尝试使用您尝试设置为DTSorting构造函数参数的相同table属性。如果此时它为null,它肯定会抛出异常。 DataView需要现有的DataTable实例才能使用。

我还应该提一下,当前的实现可能会创建多个DataTable实例,这些实例永远不会被处理掉。有一点可以肯定的是,您没有处置正在创建的DataView对象,以便获得已排序的表。

如果我是你,我会重新考虑如何绑定数据。在DataTable中存储ViewState对象是不好的做法。完整的表将在客户端和服务器之间来回串行化,从而导致性能下降。

我建议你在这里阅读:View State Overview