Aishwarya Shiva
Aishwarya Shiva

Reputation: 3406

Unable to add user controls to a panel dynamically

I have following c# code for a desktop application. It adds the controls dynamically to Panel1 by taking the values returned by the database function.

private void loadData()
        {
            string[] names = dops.get_lstMed(textBox2.Text); //fetching values from database
            MediRow[] mr = new MediRow[names.Length]; //User control array
            panel1.Controls.Clear();
            for (int i = 0; i < names.Length; i++)
            {
                mr[i] = new MediRow();
                mr[i].MedName = names[i];
                mr[i].AvailQty = dops.get_Med_qty(names[i]).ToString();
                mr[i].Quantity = "0";
                panel1.Controls.Add(mr[i]);
            }

        }

When I debugged it the values returned by the database function dops.get_lstMed() are correct and even the loop is working as it should be. But the problem is that only one control is added in the panel even database has more than one rows. Please tell me whats wrong with the code?

Upvotes: 1

Views: 1802

Answers (2)

Amedio
Amedio

Reputation: 895

Maybe this code is working but seems that is not working because you're creating them in the same position, and you have to change the Location property for the control you want to add.

At least on a first sight is the solution I can see.

Upvotes: 1

Spontifixus
Spontifixus

Reputation: 6660

@ChrisSinclair could be right: Try setting mr[i].Dock = DockStyle.Top; where you fill the control's properties.

Upvotes: 4

Related Questions