简体   繁体   English

网格视图绑定到SqlDataSource创建分页麻烦

[英]Grid View bound to SqlDataSource creating paging trouble

I am developing a web app using C#, .net 4. I have a grid view which is bound to a SqlDataSource. 我正在使用C#、. net 4开发Web应用程序。我有一个绑定到SqlDataSource的网格视图。 There are about more than one crore records of this data source. 此数据源有大约一千万笔记录。 This is because, I have allowed paging. 这是因为,我允许分页。 I have a filter criteria on the same page. 我在同一页面上有一个过滤条件。 User selects options using checkboxes. 用户使用复选框选择选项。 And then on the event of filter Button, I wrote some code which builds a new SELECT command according to the filter options for SqlDataSource. 然后,在发生过滤器按钮事件时,我编写了一些代码,根据SqlDataSource的过滤器选项构建了一个新的SELECT命令。 And after building a new query. 并在建立新查询之后。 I wrote another line of code which is: 我写了另一行代码是:

DataGridView.SelectCommand= FilteredQuery; DataGridView.SelectCommand = FilteredQuery;

So when user clicks FilterButton, the gridview shows the filtered data. 因此,当用户单击FilterButton时,gridview显示已过滤的数据。 As i told you, I have allowed Paging. 正如我告诉您的,我已允许分页。 So after filteration, when i selects the next button of that gridview, it shows the whole data instead of Filtered one. 因此,过滤后,当我选择该网格视图的下一个按钮时,它将显示整个数据,而不是已过滤的数据。 I think this is because it postsbacks the page and binds the gridview according to the predefined SELECT command for SqlDataSource. 我认为这是因为它根据SqlDataSource的预定义SELECT命令回发页面并绑定gridview。

I tried many approaches. 我尝试了许多方法。 I used static bool variable to keep track on page_load event. 我使用了静态布尔变量来跟踪page_load事件。 It works fine then. 那就很好了。 But it still troubles when i nevigate from one page to another. 但是当我从一页导航到另一页时,仍然会遇到麻烦。 When i come back to this page, it still shows the filtered data. 当我回到此页面时,它仍然显示已过滤的数据。

I have very less time. 我的时间很少。 Please guide me as soon as possible. 请尽快指导我。 Remember, I'm totally new to dot net technologies. 记住,我是点网技术的新手。 So any detailed guidance will be highly appreciated. 因此,任何详细的指导将不胜感激。 Thanks. 谢谢。

Best Regards! 最好的祝福!

You have to create OnPageIndex Changed event 您必须创建OnPageIndex Changed事件

<asp:GridView ID="yourGrid" runat="server" AutoGenerateColumns="false" OnPageIndexChanged="yourGrid_PageIndexChanged">

And in code behind 并在后面的代码中

protected void yourGrid_PageIndexChanged(object sender, EventArgs e)
{
    if (filtered) // you would have to keep track of the filtered state of your grid possibly with hidden field
    {
        DataGridView.SelectCommand = FilteredQuery;
    }
}

SQLDATASOURCE wont work this way. SQLDATASOURCE无法以这种方式工作。 On every page change, you need to recreate your sql query. 每次页面更改时,您都需要重新创建sql查询。 Try skipping certain rows on each page change. 尝试跳过每次页面更改时的某些行。

Here is an example for you. 这是给你的例子。

FOR PAGE - 1 换页-1

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY YOURKEY desc) AS rownumber,
    *
  FROM YOURTABLE
) AS ANYTHING
WHERE rownumber >= STARTING ROW and rownumber <= ENDINGROW

FOR EXAMPLE 例如

 SELECT * FROM (
      SELECT
        ROW_NUMBER() OVER (ORDER BY bowzer_id desc) AS rownumber,
        *
      FROM Bowzer
    ) AS BowzerTable
    WHERE rownumber >= 10 and rownumber <= 20

FOR PAGE - 2 页面-2

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY bowzer_id desc) AS rownumber,
    *
  FROM Bowzer
) AS BowzerTable
WHERE rownumber >= 20 and rownumber <= 30

For PAGE - 3 对于PAGE-3

SELECT * FROM (
  SELECT
    ROW_NUMBER() OVER (ORDER BY bowzer_id desc) AS rownumber,
    *
  FROM Bowzer
) AS BowzerTable
WHERE rownumber >= 30 and rownumber <= 40

You need to recreate your SQL query on every page change, as shown in example above. 如上例所示,您需要在每次页面更改时重新创建SQL查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM