DataGridView:使用数组或List <t>添加新行

Kes*_*dal 3 c# datagridview list

我的表单中有两个listbox元素.listbox1中的项表示DataGridView中的列,listbox2中的项表示DataGridView中的行.

foreach (var el in listBox1Elements)
{
// code...
dataGridView1.Columns.Add(col);
}
Run Code Online (Sandbox Code Playgroud)

由于可以添加或删除listbox1中的项目,因此我的当前解决方案存在问题.

dataGridView1.Rows.Add("test","some","data","even","more","data");
Run Code Online (Sandbox Code Playgroud)

我想知道是否有使用List的解决方案.然后我的代码看起来像这样:

foreach (var el in listBox2Elements)
{
   myList.add("some text");
}
dataGridView1.Rows.Add(myList);
Run Code Online (Sandbox Code Playgroud)

spa*_*jce 7

就像是

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

        foreach (var item in myList)
            dataGridView1.Rows.Add(item.., item.., item..);
Run Code Online (Sandbox Code Playgroud)

要么

dataGridView1.DataSource = myList.ToList()
Run Code Online (Sandbox Code Playgroud)