locked
Convert Multiple pages DataGrid to DataTable RRS feed

  • Question

  • User1944504294 posted

    Hi,

    Please help,

    How to read the all the pages in the GridView.

    Now I can able to get only the current page of the grid.

    But My Grid has 1,2,3..10 pages... (pages count changes dynamically)

    so want to count the number of pages in the Grid and to do loop in all the rows in every pages of the grid

    --------------------------------------------------------------------------------------------------------------------------------------

    // Only looping the current page of the Grid... Want to loop all the rows int he maximum ages of the grid

    foreach (GridViewRow grd1Row in gvSelected.Rows)
    {
    DataRow dtRow1 = dt.NewRow();

    dtRow1[0] = grd1Row.Cells[1].Text.Replace(" ", "");

    for (int k = 1; k < grd1Row.Cells.Count - 1; k++)
    {

    dtRow1[k] = grd1Row.Cells[k + 1].Text.Replace("&nbsp;", "");
    }

    dt.Rows.Add(dtRow1);

    }
    }
    }

    gvSelected.DataKeyNames = new string[] { ViewState["textKey"].ToString(), ViewState["valueKey"].ToString() };

    gvSelected.DataSource = dt;



    Tuesday, June 24, 2014 9:38 AM

Answers

  • User465171450 posted

    The grid is only binding to the data in the current page. It should not be binding to any other data. The grid essentially only knows about the data it is showing. Not any other page as it doesn't need any other page.

    If you want to iterate through all your data then you should do that with the data before you bind it to your grid.

    Also, doing this often defeats the purpose of paging. Paging in general is done to avoid transmitting lots of unused data from the database. If you only need 10 rows out of 100, then that's what paging is used for and it increases performance. It may be better to attempt another query to get the data you are looking for so you can have a nice, optimized query with the data you need coming from the database instead of relying on loading everything in the page and looping through it. That just adds overhead to the entire process.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2014 1:09 PM

All replies

  • User465171450 posted

    The grid is only binding to the data in the current page. It should not be binding to any other data. The grid essentially only knows about the data it is showing. Not any other page as it doesn't need any other page.

    If you want to iterate through all your data then you should do that with the data before you bind it to your grid.

    Also, doing this often defeats the purpose of paging. Paging in general is done to avoid transmitting lots of unused data from the database. If you only need 10 rows out of 100, then that's what paging is used for and it increases performance. It may be better to attempt another query to get the data you are looking for so you can have a nice, optimized query with the data you need coming from the database instead of relying on loading everything in the page and looping through it. That just adds overhead to the entire process.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 24, 2014 1:09 PM
  • User-1716253493 posted

    why not get it from dt before/after grid binding

    Wednesday, June 25, 2014 12:24 AM
  • User-1557429034 posted

    first disable paging in your c#

    grd1Row.AllowPaging = false;
    
    foreach (GridViewRow grd1Row in gvSelected.Rows)
    {
    DataRow dtRow1 = dt.NewRow();
    
    dtRow1[0] = grd1Row.Cells[1].Text.Replace("&nbsp;", "");
    
    for (int k = 1; k < grd1Row.Cells.Count - 1; k++)
    {
    
    dtRow1[k] = grd1Row.Cells[k + 1].Text.Replace("&nbsp;", "");
    }
    
    dt.Rows.Add(dtRow1);
    
    }
    }
    }
    
    gvSelected.DataKeyNames = new string[] { ViewState["textKey"].ToString(), ViewState["valueKey"].ToString() };
    
    gvSelected.DataSource = dt;



    Thursday, June 26, 2014 1:04 AM