Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind gridview to allow paging when datasource is Datatable generated by cloning in asp.net

Tags:

c#

asp.net

I have a gridView

<asp:GridView ID="InGridView" PageSize="5" AllowPaging="true"  runat="server" OnPageIndexChanging="InGridView_PageIndexChanging" ></asp:GridView>  

code binding to grid.

private void LoadGridView(string filename)
{
    invTable = new DataTable();
   // gets data from uploaded csv file
  DataTable csvDatatable=GetdataFromCSV(string filename)
    // Code to populate invTable with the data..
    invTable= checkCsvandGetTable(csvDatatable)
    InGridView.DataSouce = invTable;
    inGridView.DataBind();

} where data binding is being done on button_click and datasource is datatable which i am generating from csv file.

how to allow paging in such scenario

the invTable is null when i am selecting 2 page for first page its working fine.

any idea on this?

like image 363
Harshit Avatar asked Nov 26 '25 01:11

Harshit


1 Answers

You need to load invTable with the data again and bind it to the GridView in PageIndexChanged event.

Let say you have a method which creates a datatable, populates it with data and binds it to the gridview. And you are calling this method from the button click.

private void LoadGridView()
{
    invTable = new DataTable();
    // Code to populate invTable with the data..
    InGridView.DataSouce = invTable;
    inGridView.DataBind();
}

You need to call the same method from the PageIndexChanged event handler. So, PageIndexChanged eventhandler of InGridView should be written as following.

protected void InGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    InGridView.PageIndex = e.NewPageIndex;
    LoadGridView();
}

This should resolve your issue.

like image 76
Chetan Avatar answered Nov 27 '25 14:11

Chetan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!