Asp.net 启用分页时如何获取Gridview的所有行??

Asp.net 启用分页时如何获取Gridview的所有行??,asp.net,Asp.net,启用分页时如何获取gridview的所有行 //Get Current Page Index so You can get back here after commands int a = GridView1.PageIndex; //Loop through All Pages for (int i = 0; i < GridView1.PageCount; i++) { //

启用分页时如何获取gridview的所有行

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

它只允许获取当前的获取行,而不是整个gridview行。

我们暂时禁用分页,并将重新绑定网格,以便现在我们可以访问数据源中的所有记录,而不仅仅是当前页面记录

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
一旦gridview与所有记录绑定,就可以遍历gridview行

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
完成任务后,我们将重新启用分页并重新绑定网格

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
以下是应对您的情况的方法:

protected void Page_Load(object sender, EventArgs e)
{
    GridView2.AllowPaging = false;
    GridView2.DataBind(); 

    // You can select some checkboxex on gridview over here..

    GridView2.AllowPaging = true;
    GridView2.DataBind(); 
}
//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

更好的方法是在页面顶部(gridview之外)放置一个隐藏字段,单击复选框后,您应该在隐藏字段中以逗号分隔的格式放置相关id或某些值。提交表单时,您可以使用逗号作为分隔符拆分隐藏的字段值字符串。使用以下代码并禁用GridView分页

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
GridView1.AllowPaging=false; GridView1.DataBind()

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

页面加载或某些其他事件中,如果您希望显示所有Gridview行,则启用分页时无法显示所有行。但您可以将
Allowpaging=false

protected void Page_Load(object sender, EventArgs e)
{
Gridviewname.AllowPaging=false;
}
//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

在从网格中获取数据之前,只需编写

yourGridName.AllowPaging=false;
//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
在数据写入之后

yourGridName.AllowPaging=true;
//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
如果您的函数是GetDataFromGrid(),那么您应该这样做

protected void Page_Load(object sender, EventArgs e)
{
yourGridName.AllowPaging=false;
GetDataFromGrid() 
yourGridName.AllowPaging=true;
}
//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);

您可以使用下面的命令,我正在项目中使用这些命令。它的逻辑很简单,你浏览所有的页面,在每一页你浏览所有的行。您还可以在执行此操作之前获取当前页面,并在循环所有内容之后返回该页面;)

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
//获取当前页面索引,以便在执行命令后返回此处
int a=GridView1.PageIndex;
//循环浏览所有页面
对于(int i=0;i
使用以下代码获取
e.CommandArgument
的值。我一定会解决你的问题

//Get Current Page Index so You can get back here after commands
                int a = GridView1.PageIndex;
    //Loop through All Pages
                for (int i = 0; i < GridView1.PageCount; i++)
                {
    //Set Page Index
                    GridView1.SetPageIndex(i);
    //After Setting Page Index Loop through its Rows
                    foreach (GridViewRow row in GridView1.Rows)
                    {
                        //Do Your Commands Here
                    }
                }
    //Getting Back to the First State
                GridView1.SetPageIndex(a);
  protected void GridViewID_RowCommand(object sender, GridViewCommandEventArgs e)
  {
         if (e.CommandName == "GetDetail")
         {
            int index = Convert.ToInt32(e.CommandArgument) % GridViewID.PageSize; // !Important

            GridViewRow row = GridViewID.Rows[index];
         }
 }

难道你不能从
数据源中获取所有行吗?这意味着你想知道如何禁用分页吗?;)这背后的目的是什么..?实际上gridview将出现在pageload上,然后我在gridview上选择一些复选框。。。然后我将进入gridview的第二页,我尝试选择其他复选框,但当我保存所有数据时,它只保存当前页面数据,而不是所有gridview页面。我将获得所有选中该复选框的行。grdSupplierSearch.Rows.Cast()。其中(a=>((复选框)a.FindControl(“ckbSelectCompany”)。选中和选中的公司名称。包含((标签)a.FindControl(“lblCompanyNameGrid”).Text)).ToList();但它只允许保存当前页面数据,而不允许保存gridview的其他页面。谢谢@Vishal Suthar,完成了。如果它真的有帮助,那么您可以接受答案。请参阅:()是的,如果不调用databind(),则无法工作。谢谢,这对我也有帮助。这种方法有效,但使用起来有风险。如果有人对代码做了任何更改,但没有意识到分页在哪里被关闭,则可能会使某些人的web浏览器崩溃。设置页面索引更安全。这是更简单的方法。关闭或打开AllowPaging都充满了问题,尤其是如果您不想打乱客户端正在查看的页面(看到GridView突然扩展和收缩看起来不太好),如果您在所有页面上都有复选框,并且有些页面被勾选或未勾选,该怎么办?还有很多,ViewState控制一切会变得混乱。到目前为止,这是更好的答案。我甚至想说唯一的答案。不要试图轻视公认的回答者,因为它从技术上回答了问题,但这是一种不太“粗鲁”的方式,也是一种更专业的做事方式。任何阅读这篇文章的人都应该在接受答案的基础上使用这种方法。别怀疑。