【问题标题】:DataTable must be set prior to using DataView必须在使用 DataView 之前设置 DataTable
【发布时间】:2014-04-10 16:16:49
【问题描述】:

您好,我确实查看了“必须在使用 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;
    }
}

【问题讨论】:

  • 您的Data-source 似乎为空

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


【解决方案1】:

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

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

如果我是你,我会重新考虑应该如何绑定数据。在ViewState 中存储DataTable 对象是不好的做法。全表会在客户端和服务器之间来回序列化,导致性能损失。

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

【讨论】:

  • 谢谢,但这并不能真正解决我的问题
  • @Deviney 你是说DTSorting 属性永远不会返回null
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多