Link to home
Start Free TrialLog in
Avatar of wenyonk
wenyonk

asked on

HOW TO: populate indvidual controls with data from array

I have a groupbox with 10 (0-9) command buttons (btn0, btn1...btn9) in it.

I have an array of data that may have more than 10 items in it, but for now lets assume that the array has 10 or less items.  I would like to be able to iterate through the controls based on their placement or tab index and assign properties from the array to each command button.  Below is the code I have so far.  Can someone please help??

private void LoadPOSTransationTypes()
{
    try
    {
        // declare array and fill it with data from DB
        ItemX[] itms = Utilities.GetItemXs();
        // if there items returned continue
        if (itms.Length > 0)
        {
            // iterate through the controls in the groupbox
            foreach (Control control in this.groupBox.Controls)
            {
                    // if the control is a button
                    if (control is Button)
                    {
                        // set the properties of the button with the item in the array here
                    }
            }
        }
        else
        {
             // throw some type of exception, data not found            
        }
    }
    catch (Exception ex)
    {
    }
    finally
    {
     }
}

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Assuming they all start with "btn", followed by a number, then a simplistic approach would be to parse the number out of the controls name and then use that as in Index to grab data from your array.

Something like...

    int index = int.Parse(control.Name.Substring(3));
    control.Text = itms[index].someThing;
Avatar of Seraph_78
Seraph_78

You could use the Tag property of the button to assign it the appropriate array index that contains the properties for that button.  That way you can name all of the buttons whatever you like.

So set btn0.Tag = 0, btn1.Tag = 1, etc.

Then in your if statement all you have to do is:

If(control is Button)
{
   control.SomeProperty = itms[control.Tag].SomeProperty;
}


Or you can use the Control.GetNextControl method to go over the controls in the tab order. You only need to know the first control. More info: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol.aspx
how is your relation between buttonX and itemX?
always 1 to 1 or could it be 1 to n ?
if you got your db data, does it come in the correct order like your button order?
or does your db data have a unique id?
if so, you will only have to save that unique id to your buttons tag object and if your relation between buttonX and itemX is 1 to n, i would recommend using a hashtable where the key is your unique id and your values are the items for that key.
Avatar of wenyonk

ASKER

Essentially:

I fill the array from the DB in the desired display order.  Item1 should be assigned to button1, item2 to button2, and so on.  if there are 5 items returned to from the DB to the array I want to display data in the first 5 buttons and disable the remaining 5 buttons.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wenyonk

ASKER

Additional Questions Please see my next post  
Avatar of wenyonk

ASKER

Title: HOW TO: populate indvidual controls with data from array  Part2