Search code examples
c#asp.net.netdatatabledatapager

how to fill a datatable so that you can iterate through


How to I create a Next and Previous button and loop through my query which has been returned to a datatable?

I have tried the following:

protected void btnPrevious_Click(object sender, EventArgs e)
{
    DAL.CustomersDataSetTableAdapters.CustomerDetailsTableAdapter eobj = new DAL.CustomersDataSetTableAdapters.CustomerDetailsTableAdapter();
    DataTable dt = new DataTable();
}

I need to loopthrough the values which will be returned in text boxes on the form.


Solution

  • After reading through the comments, it seems that you're looking to have a Next and Previous button to go through the records in a data table, which is already returned by running a query to generate the datatable.

    What I would do is:

    1. Create a separate function that runs the query and initializes the table.

    You probably don't want to always create a new datatable and data-bind to it whenever the user clicks the previous button (looks like that's what you've been attempting to do up there)

    So roughly something like:

    DataTable dt; //declare first so other fucntions can use this
    
    protected void initializeDataTable()
    {
        DAL.CustomersDataSetTableAdapters.CustomerDetailsTableAdapter eobj = new DAL.CustomersDataSetTableAdapters.CustomerDetailsTableAdapter();
        dt = new DataTable();
        //you might have to bind to your datasource, or 
        //run the query that generates the dataTable here.
    
        //after all that you might want to display the first value, index 0. 
    }
    

    I am not familiar with ASP.net DataTable bindings, this tutorial might help. Basically you need to load the data in, which I think you can handle that part.

    1. Next and Previous Buttons will increment/decrement a variable.

    Create a new value i in the class, and initialize it to 0. When the user clicks on the next button, i++, and when the user clicks on the previous button i--, you would have to make sure that it does not go below 0 or more than the length of the dataTable.

    Something like;

    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        if (i < totalNumberOfRecords) 
        {
            i = i + 1;
            DisplayResults(i);
    
            LinkButton btnPrevious = (LinkButton)Master.FindControl("ContentController").FindControl("btnPreviousRecord");        btnPrevious.Enabled = true; 
     }
    }
    

    We make btnPrevious.Enabled = true or false because we don't want the user to keep on clicking the Previous button even if it is already at the top.

    You could also add first and last buttons too. For first set index to 0, for last set index to the maximum number of rows there are.

    The DisplayResults function looks like this:

    public void DisplayResults(int rowNumber)
    {
       textBox1.Text = ds.Tables[0].Rows[i]["ID"].ToString();
       textBox2.Text = ds.Tables[0].Rows[i]["empname"].ToString();
       textBox3.Text = ds.Tables[0].Rows[i]["salary"].ToString();
       //and so on so forth.
    }
    

    The example was adapted from this C# tutorial, perhaps that might be of assistance to you.