1

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?

4
  • yes, and the same code is working for a web-application but not for WinForms. Commented Sep 18, 2012 at 11:06
  • 2
    The controls aren't being stacked/superimposed on top of each other, are they? Commented Sep 18, 2012 at 11:07
  • Silly question: are you sure there is only one control added or they're all added but piled one upon the other at the same coordinates, so you only see the last one? Do the controls have a visible label or text? That way you could set that text to the value of i, for example, and check its value... Commented Sep 18, 2012 at 11:08
  • Are you sure it is not adding all the controls, but they are being shown at the same location, so you can only see the top one? Commented Sep 18, 2012 at 11:08

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.