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.
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:
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:
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.
Create a new value
iin the class, and initialize it to 0. When the user clicks on the next button,i++, and when the user clicks on the previous buttoni--, you would have to make sure that it does not go below 0 or more than the length of the dataTable.Something like;
We make
btnPrevious.Enabled = trueor 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:
The example was adapted from this C# tutorial, perhaps that might be of assistance to you.