How to create dynamic buttons based on an arraylist
<p>I'm having some trouble creating a foreach loop that creates buttons dynamically based on a List that is inside the NamesDA class.</p>

<p>I'm getting errors such as: Cannot convert type 'Program1.Names' to 'int'. I've tried what I know to fix the conversion error, but I don't know the correct way to do it.</p>

<p><strong>Edit 1:</strong> allNames is an array list inside NamesDA that reads a csv file.
It returns a list of strings and int's, which then they are to be used to create the buttons and represent them.</p>

<p><strong>Edit 2:</strong> The foreach loop problem is solved now, but I'm unable to get the values of column[0] for button text and column[1] for button tag. </p>

<p>The NamesDA class:</p>

<pre><code>private const string path = "names.csv";
public static List&lt;Names&gt; GetNames()
{
   StreamReader textIn = new StreamReader(new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));
   List&lt;Names&gt; allNames = new List&lt;Names&gt;();
   while (textIn.Peek() != -1)
   {
       string row = textIn.ReadLine();
       string[] columns = row.Split(',');
       allNames.Add(new Names(columns[0].ToString(), Convert.ToInt16(columns[1])));
   }

   textIn.Close();
   return allNames;
}   
</code></pre>

<p>The form:</p>

<pre><code>int startTop = 20;
int startLeft = 17;

allNames = NamesDA.GetNames(); //calling the method in the NamesDA class
foreach (int x in allNames) {
    names[x] = new Button();
    tempButton.Text = ""; //based on the list column[0]
    tempButton.Tag = "";  //based on the list column[1]
    names[x].Location = new System.Drawing.Point(startTop + (x * 95), startLeft);    
    listView.Controls.Add(names[x]);
}
</code></pre>