无法使用 C# 对 ASP.NET 2.0 中由 DataView 填充的 GridView 进行排序

发布于 2024-10-03 19:48:39 字数 4910 浏览 9 评论 0原文

我正在致力于使用 InterBase 数据库扩展 ASP.NET 2.0 应用程序。我的经验是 PHP/MySQL,所以我对 ASP 的熟悉程度目前在 2 周范围内,并且是通过检查同事的代码、我的 ASP 书的前 90 页和 Google 拼凑而成的。在应用程序中,我有一个 SqlDataSource 控件连接到我的数据库并选择我需要的信息。然后,结果被复制到 DataView 中,我在其中修改其中一列中的数据,然后将该 DataView 推送到 GridView 进行输出。我遇到的问题是我现在无法对 GridView 进行排序。我按照此处的说明进行操作:http://forums.asp.net/p/956540/1177923。 aspx,但无济于事。

这是页面代码:

    <form id="form1" runat="server">
<div>
    <asp:SqlDataSource ID="Products" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
        ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" OnLoad="ProductsDS_Load"
        OnSelected="ProductsDS_Selected" DataSourceMode="DataSet">
    </asp:SqlDataSource>

    <br />
    <asp:Label ID="testlabel" runat="server"></asp:Label>
    <br />
    <asp:Label ID="testlabel2" runat="server"></asp:Label>
    <br /><br />
    This table lists all 2000+ numbered projects which are at least partially in process.<br />
    The Project number link leads to a more detailed view of that project.<br />
    <br />
    <asp:Label runat="server" ID="numrows"></asp:Label> results returned.
    <br />
    <asp:GridView ID="ProductsView" runat="server" EnableModelValidation="True" 
        AutoGenerateColumns="False" CellPadding="4" OnSorting="ProductsView_Sorting"
        ForeColor="#333333" GridLines="None" AllowSorting="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:HyperLinkField HeaderText="Project" SortExpression="PROJECT"
                DataTextField="PROJECT" Target="subweeklyreport" DataNavigateUrlFields="PROJECT"
                DataNavigateUrlFormatString="Products.aspx?p={0}" />
            <asp:BoundField Visible="false" DataField="PROJECTID" />
            <asp:BoundField DataField="PART" HeaderText="Part #" 
                SortExpression="PART" />
            <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" 
                SortExpression="DESCRIPTION" />
            <asp:BoundField DataField="ENGMGR" HeaderText="Eng. Mgr." 
                SortExpression="ENGMGR" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>
</div>
</form>

这是背后的代码:

protected void ProductsDS_Load(object sender, EventArgs e)
{

    string SQLQuery = Query would go here;

    testlabel2.Text = SQLQuery;
    Products.SelectCommand = SQLQuery;
    Products.DataBind();

    DataView dv = (DataView)Products.Select(new DataSourceSelectArguments());

    foreach (DataRow dr in dv.Table.Rows)
    {
        string name = dr["ENGMGR"].ToString();
        string[] explode = name.Split(' ');
        string newname;
        if (explode.Length == 3)
        {
            newname = explode[2] + ", " + explode[0];
        }
        else
        {
            newname = explode[1] + ", " + explode[0];
        }

        dr["ENGMGR"] = newname;
        //testlabel.Text = dr["ENGMGR"].ToString();
    }


    Products.DataBind();
    //ProductsView.DataSourceID = "Products";
    ProductsView.DataSource = dv;
    ProductsView.DataBind();
    ProductsView.Enabled = true;
    ProductsView.Visible = true;

}

protected void ProductsDS_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    numrows.Text = e.AffectedRows.ToString();
}

protected void ProductsView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = ProductsView.DataSource as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        ProductsView.DataSource = dataView;
        ProductsView.DataBind();
    }
}

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

        case SortDirection.Descending:
            newSortDirection = "DESC";
            break;
    }

    return newSortDirection;
}

我认为发生的情况是,每当 GridView 进行排序回发时,它都会导致再次执行查询并覆盖对 GridView 中现有数据进行排序的尝试,但我现在对 ASP 的了解还不够,无法防止这种行为。任何帮助将不胜感激。

I am working on extending an ASP.NET 2.0 application using an InterBase database. My experience is in PHP/MySQL, so my familiarity with ASP is currently in the 2-week range, and is pieced together from examining a co-worker's code, the first 90 pages of my ASP book, and Google. In the application, I have an SqlDataSource control connecting to my database and selecting the information I need. Then the results are copied into a DataView where I modify the data in one of the columns, and then I push that DataView to a GridView for output. The issue I'm having is that I cannot sort the GridView at this point. I followed instructions here: http://forums.asp.net/p/956540/1177923.aspx, but to no avail.

Here is the page code:

    <form id="form1" runat="server">
<div>
    <asp:SqlDataSource ID="Products" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
        ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" OnLoad="ProductsDS_Load"
        OnSelected="ProductsDS_Selected" DataSourceMode="DataSet">
    </asp:SqlDataSource>

    <br />
    <asp:Label ID="testlabel" runat="server"></asp:Label>
    <br />
    <asp:Label ID="testlabel2" runat="server"></asp:Label>
    <br /><br />
    This table lists all 2000+ numbered projects which are at least partially in process.<br />
    The Project number link leads to a more detailed view of that project.<br />
    <br />
    <asp:Label runat="server" ID="numrows"></asp:Label> results returned.
    <br />
    <asp:GridView ID="ProductsView" runat="server" EnableModelValidation="True" 
        AutoGenerateColumns="False" CellPadding="4" OnSorting="ProductsView_Sorting"
        ForeColor="#333333" GridLines="None" AllowSorting="True">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:HyperLinkField HeaderText="Project" SortExpression="PROJECT"
                DataTextField="PROJECT" Target="subweeklyreport" DataNavigateUrlFields="PROJECT"
                DataNavigateUrlFormatString="Products.aspx?p={0}" />
            <asp:BoundField Visible="false" DataField="PROJECTID" />
            <asp:BoundField DataField="PART" HeaderText="Part #" 
                SortExpression="PART" />
            <asp:BoundField DataField="DESCRIPTION" HeaderText="Description" 
                SortExpression="DESCRIPTION" />
            <asp:BoundField DataField="ENGMGR" HeaderText="Eng. Mgr." 
                SortExpression="ENGMGR" />
        </Columns>
        <EditRowStyle BackColor="#7C6F57" />
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#E3EAEB" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>
</div>
</form>

And here is the code behind:

protected void ProductsDS_Load(object sender, EventArgs e)
{

    string SQLQuery = Query would go here;

    testlabel2.Text = SQLQuery;
    Products.SelectCommand = SQLQuery;
    Products.DataBind();

    DataView dv = (DataView)Products.Select(new DataSourceSelectArguments());

    foreach (DataRow dr in dv.Table.Rows)
    {
        string name = dr["ENGMGR"].ToString();
        string[] explode = name.Split(' ');
        string newname;
        if (explode.Length == 3)
        {
            newname = explode[2] + ", " + explode[0];
        }
        else
        {
            newname = explode[1] + ", " + explode[0];
        }

        dr["ENGMGR"] = newname;
        //testlabel.Text = dr["ENGMGR"].ToString();
    }


    Products.DataBind();
    //ProductsView.DataSourceID = "Products";
    ProductsView.DataSource = dv;
    ProductsView.DataBind();
    ProductsView.Enabled = true;
    ProductsView.Visible = true;

}

protected void ProductsDS_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    numrows.Text = e.AffectedRows.ToString();
}

protected void ProductsView_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dataTable = ProductsView.DataSource as DataTable;

    if (dataTable != null)
    {
        DataView dataView = new DataView(dataTable);
        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        ProductsView.DataSource = dataView;
        ProductsView.DataBind();
    }
}

private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
    string newSortDirection = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            newSortDirection = "ASC";
            break;

        case SortDirection.Descending:
            newSortDirection = "DESC";
            break;
    }

    return newSortDirection;
}

What I think is happening is that whenever the GridView does the postback for the sort, it causes the query to be executed again and overwrite the attempt to sort the existing data in the GridView, but I don't know enough about ASP right now to prevent this behavior. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-10-10 19:48:39

我最终解决了我自己的问题。我创建了一个会话变量来存储页面加载之间的数据视图,并在执行查询之前检查数据视图是否已存储,如果是则对其进行排序,否则仅执行常规查询。由于我不希望在初始页面视图和排序之间引入数据,因此我认为使用数据的存储副本不会是一个主要问题。

I ended up solving my own problem. I created a session variable to store the dataview between page loads, and checking to see if the dataview is stored before executing the query, and sorting it if it is, and just performing the regular query otherwise. Since I don't expect data to be introduced between the initial page view and the sort, I don't think using a stored copy of the data would be a major issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文