Is there a way to "clear a previous loop and form a fresh new loop"? I'm paging my datagrid. Upon form_load, it shows all records in batches of for example 10, which I can set in a textbox. If for example I have 60 records, then my loop code produces lblpage.Text 1 2 3 4 5 6. These labels when clicked, show the correct batch of records in the datagrid. Now, if I click letter A to mean show all records that start with A (for example it has 15 records), then that same loop code should produce lblpage.Text 1 2.

Here's my code:

private void loadgridview(string gridviewquery,string countquery)

            Class1.displayempl(dataGridViewempl, gridviewquery); //load gridview
            //create page numbers
            int x = 103;
            int y = 6;
            int recordcount = getnumofrecords(countquery);
            int pagecount = (recordcount/int.Parse(txtpagesize.Text)); 
            if ((recordcount % int.Parse(txtpagesize.Text)) > 0) //to compute for partial page if any
                pagecount++;  //if partial page, make it still 1 page
            lblpage = new Label[pagecount+1];
            for (int i = 1; i <= pagecount; i++)
                lblpage[i] = new Label();
                lblpage[i].Text = i.ToString();
                lblpage[i].Location = new Point(x, y);
                lblpage[i].Size = new Size(15, 13);
                lblpage[i].ForeColor = Color.Blue;
                lblpage[i].Cursor = Cursors.Hand;
                lblpage[i].Font = new Font(this.Font, FontStyle.Underline);
                lblpage[i].Click += new System.EventHandler(lblpage_Click);
                x += 25;


//upon form_load
private void FormMain_Load(object sender, EventArgs e)

loadgridview("Select TOP " + int.Parse(txtpagesize.Text) + " * from dbo.displayallempl()", "Select COUNT(*) from dbo.displayallempl()");

//upon clicking for example letter A
private void lblalpha_Click(object sender, EventArgs e)
letter = sender as Label;
loadgridview("Select TOP " + int.Parse(txtpagesize.Text) + " * from dbo.displayemplbylastname('" + letter.Text + "')", "Select COUNT(*) from dbo.displayemplbylastname('" + letter.Text + "')");

My problem is: upon clicking lblalpha, lblpage.Text still shows the same number of pages upon form_load, i.e. from my example of 15 records above, it still shows 1 2 3 4 5 6, not 1 2; although when I click any of the labels 3 4 5 6, the datagrid already shows blank (which is partly correct).

Appreciate any help. Thanks much in advance.

"Clear the loop". I knew it was silly :=) I was adding "Controls" so what I needed was to clear them. Of course. What a blooper :=) Found my solution. I have to add this into my lblalpha_Click (before the loadgridview line):

for (int i = pagecount; i >= 1; i--)

To all who took peeks into this post (and were probably shaking their heads at the silliness), thanks for your time :=)

This question has already been answered. Start a new discussion instead.