C# 如何使用Windows应用程序在DataGridView中添加多个项?

C# 如何使用Windows应用程序在DataGridView中添加多个项?,c#,windows,combobox,C#,Windows,Combobox,我刚刚开始使用dataGridView 这里我在Windows应用程序c#中使用Combobox和DataGridView 我们在组合框中选择了将在中逐个添加项目的任何项目 DataGridView单元格 所选项目将添加到[customername]列中 例如: private void Form_Load(object sender, EventArgs e) { Combobox1.Items.Add("Name1"); Combobox1.Items.Add(

我刚刚开始使用dataGridView

这里我在Windows应用程序c#中使用Combobox和DataGridView

我们在组合框中选择了将在中逐个添加项目的任何项目 DataGridView单元格

所选项目将添加到
[customername]
列中

例如:

private void Form_Load(object sender, EventArgs e)
{
       Combobox1.Items.Add("Name1");
       Combobox1.Items.Add("Name2");
       Combobox1.Items.Add("Name3");
       Combobox1.Items.Add("Name4");
}
private void cmbpaymentopt_SelectedIndexChanged(object sender, EventArgs e)
    {
        string item = cmbpaymentopt.SelectedItem.ToString();
        if (item != null)
        {
           //Here Code 
        }
    }

可以使用DataSource属性设置DataGridView中的行

dataGridView1.DataSource = _listCustomers;
编辑:

如果dataGridView使用绑定,则使用

private static void AddItemToGrid<T>(DataGridView dataGridView, T data)
{
    var dataSource = dataGridView.DataSource as BindingSource;
    dataSource?.Add(data);
}

这里我没有使用DataSource。我只是在Form_load event中添加项,我们在组合框中选择任何项,该项将添加到DataGridView列
private static void AddItemToGrid(DataGridView dataGridView, params object[] cells)
{
    dataGridView.Rows.Add(cells);
}