I would like to iterate through a datatable of rows using Next and Previous buttons.

My form has the following:

[Ticket Ref]
[Short Description]
[Next Step]
[Last Updated]

Through the life of a case, you might get multiple updates. Therefore I may have 5 rows in the database relating to a particular Ticket Reference. Comments and Last Updated will obviously be different.

When the form loads it will display the last record in the database into the text boxes i.e the last row from the query. I would then like to click Previous and see the previous rows. But if i click on Next I expect it to loop through. i.e go to the next record. So if im on record 1, then it needs to go to 2.

I have tried counting the clicks but this isnt very helpful as when the form first loads count will be 0 and if i click Previous then i will get a Row out of position -1 error.

My previous button looks like this:

    protected void btnPrevious_Click1(object sender, EventArgs e)
    {
        DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
        DataTable dt = new DataTable();
        dt = eobj.GetTicketUpdates(txtSupportRef.Text);

        int i = 0;

        if (i < dt.Rows[0].Table.Rows.Count - 1 || i != 0)
        {
            i--;
            txtShortDesc.Text = dt.Rows[0].Table.Rows[i]["ShortDesc"].ToString();
            txtNextStep.Text = dt.Rows[0].Table.Rows[i]["NextStep"].ToString();
            txtLastUpdated.Text = dt.Rows[0].Table.Rows[i]["LastUpdated"].ToString();
        }
        else
        {
            //no records to see more.
        }
    }

My next button looks like this:

    protected void btnNext_Click1(object sender, EventArgs e)
    {
        DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter eobj = new DAL.TicketsDataSetTableAdapters.TicketDetailsTableAdapter();
        DataTable dt = new DataTable();
        dt = eobj.GetTicketUpdates(txtSupportRef.Text);

        int i = 0;

        if (i < dt.Rows[0].Table.Rows.Count - 1)
        {
            i++;
            txtShortDesc.Text = dt.Rows[0].Table.Rows[i]["ShortDesc"].ToString();
            txtNextStep.Text = dt.Rows[0].Table.Rows[i]["NextStep"].ToString();
            txtLastUpdated.Text = dt.Rows[0].Table.Rows[i]["LastUpdated"].ToString();
        }
        else
        {
            //no records to see more.
        }
    }

Also the user will have the ability to load different ticket references into the form. So i need the ability to quickly iterate based on the loaded ticket.

If you need more info please ask.

Related posts

Recent Viewed