Kesandal Kesandal - 2 years ago 144
C# Question

DataGridView: Add new row using an array or List<t>

I've two listbox-elements in my form.
The items in listbox1 are representing the columns in my DataGridView,
the items in my listbox2 are representing the rows in my DataGridView.

foreach (var el in listBox1Elements)
// code...

Since items in listbox1 can be added or deleted I've a problem with my current solution.


I wonder if there is a solution for using a List.
Then my code could look something like this:

foreach (var el in listBox2Elements)
myList.add("some text");

Answer Source

Something like

        foreach (var el in listBox2Elements)
            myList.add("some text");

        foreach (var item in myList)
            dataGridView1.Rows.Add(item.., item.., item..);


dataGridView1.DataSource = myList.ToList()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download