C# 如何以编程方式将行插入DataGridView?

C# 如何以编程方式将行插入DataGridView?,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图以编程方式将一行插入DataGridView。各栏分别为: 第1列组合框 第2列文本框 这是我的密码。Column1使用DataGridView的内置数据源绑定到表 DataGridViewRow row = new DataGridViewRow(); DataGridViewComboBoxCell __action1 = new DataGridViewComboBoxCell(); DataGridViewTextBoxCell __site = new DataGridViewT

我试图以编程方式将一行插入DataGridView。各栏分别为:

第1列组合框

第2列文本框

这是我的密码。Column1使用DataGridView的内置数据源绑定到表

DataGridViewRow row = new DataGridViewRow();
DataGridViewComboBoxCell __action1 = new DataGridViewComboBoxCell();
DataGridViewTextBoxCell __site = new DataGridViewTextBoxCell();

__action.Value = 1; //1 is id.
__sitetype.Value = "google.com";

但是这给了我一个“DataGridViewComboBoxCell无效值”的错误。我如何解决这个问题?

还需要设置combobox值,怎么做?是的,在添加每个控件之前,您应该设置它。您的
DataGridView
是否绑定到数据源?如果是这样,则将行添加到数据源,而不是控件。只有组合框绑定到数据源,以便它可以填充列表,文本列将从代码中设置。
DataGridViewRow dr = new DataGridViewRow();
DataGridViewCell dcDescription = new DataGridViewTextBoxCell();

dcDescription.Value = "some text";

dr.Cells.Add(dcDescription);
dataGridViewDoctorEventsAddServices.Rows.Add(dr);