7

How to Fetch All The rows of gridview when paging is enabled?.

it is allowing to fetch only current fetch rows not entire gridview rows.

6
  • Can't you get the Datasource where you can get all the rows? Feb 15, 2013 at 9:32
  • 1
    Does that mean you want to know how to disable paging? ;) Feb 15, 2013 at 9:32
  • 2
    What is the purpose behind this..? Feb 15, 2013 at 9:33
  • actually gridview is coming on pageload then i m selecting some checkboxex on gridview... and then i am going to second page of gridview and i m trying to select other check boxes but when i m saving all data it is saving only current page data not all gridview page
    – Kunal Shah
    Feb 15, 2013 at 9:33
  • i m getting all rows which has checked box has checked by using. grdSupplierSearch.Rows.Cast<GridViewRow>() .Where(a => ((CheckBox)a.FindControl("ckbSelectCompany")).Checked && selectedCompanyNames.Contains(((Label)a.FindControl("lblCompanyNameGrid")).Text)) .ToList(); but it is allowing to save only current page data which are checked not another pages of gridview.
    – Kunal Shah
    Feb 15, 2013 at 9:36

8 Answers 8

13

You can use the Commands Below which I'm using it in my projects. Its logic is so simple, you go through all pages and in every page you go through all rows. You can also get your current page before doing this and after looping all you can get back there ;)

//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);
3
  • 1
    This is the simpler approach. Turning OFF or ON the AllowPaging is fraught with issues, especially if you don't want to upset the page the client is seeing (would not look nice seeing a GridView expand and shrink all of a sudden), and what if you have checkboxes across all the pages and some are ticked and not ticked? There's a lot more to it and it gets messy with ViewState to control it all.
    – Fandango68
    Apr 3, 2017 at 0:05
  • This is, by far, the better answer. I'd go as far to say the only answer. Not trying to slight the accepted answerer, because it technically answers the question, but this is way less 'hacky' and a way more professional way of doing things. Anyone reading this thread should absolutely use this method over the accepted answer. Don't even question it. Jan 31, 2019 at 20:49
  • This is great. Better than messing with paging Sep 10, 2021 at 19:57
8

We disable paging temporarily and will re-bind the grid so that now we have access to all records in the datasource not just current page records.

Once gridview is binded with all records, you can iterate through gridview rows.

Once we are done with our task, we re-enable paging and rebind the grid.

Here the way to tackle with your condition:

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(); 
}
3
  • Thanks @Vishal Suthar It is Done.
    – Kunal Shah
    Feb 15, 2013 at 9:43
  • Yep, doesnt work without calling databind(). Thanks, this helped me aswell. Nov 4, 2013 at 12:47
  • This method works, but is risky to use. If someone makes any changes to the code and doesnt realize where the paging is turned off, you could crash some people's web browsers. The setting page index is safer imo. Jun 22, 2017 at 18:59
6

Use Following Code And make GridView paging disable

GridView1.AllowPaging = false; GridView1.DataBind();

on Page Load or Some Other event where You Want To show All Your Gridview Rows

4

Before the function of getting the Data from grid just write

yourGridName.AllowPaging=false;

and after getting the Data write

yourGridName.AllowPaging=true;

If your function is GetDataFromGrid() then you should go like this

protected void Page_Load(object sender, EventArgs e)
{
yourGridName.AllowPaging=false;
GetDataFromGrid() 
yourGridName.AllowPaging=true;
}
3

You cant display all rows when paging is enabled.But u can make Allowpaging=false; in codebehind in pageload or in some event..

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

or

Protected Void some event(object sender,Eventargs e)
{
Gridviewname.AllowPaging=false;
}
2

Better way is to put a hidden field on the top of the page(outside of gridview) and on click of checkboxes, you should put the related id or some value in comma separated format in hidden field. On submit of the form, you can split the hidden field value string with comma as delimiter and there you go.

0

The solution proposed by "Inside Man" with Page Index is logically correct. But you don't have to use GridView1.SetPageIndex(i); because it just sets the starting row of the current page. You should directly modify GridView1.PageIndex = i;

1
0

Use following code to get value of e.CommandArgument. It will solve your issue surely!

  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];
         }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.