效果图如下
先在界面上添加DataGridView,添加字段no、type、param
在lord事件中新建一行
dataGridView1.Rows.Add();
在RowsAdded事件中操作新建的行
private void dataGridView1_RowsAdded_1(object sender, DataGridViewRowsAddedEventArgs e)
{
//获取当前行
DataGridViewRow newRow = dataGridView1.Rows[e.RowIndex];
DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
BindCombobox(comboBoxCell);
newRow.Cells["type"] = comboBoxCell;
}
public void BindCombobox(DataGridViewComboBoxCell comboBoxCell)
{
DataTable stepType = new DataTable();
stepType.Columns.Add("Value");
stepType.Columns.Add("Name");
DataRow type;
type = stepType.NewRow();
type[0] = 1;
type[1] = "type1";
stepType.Rows.Add(type);
type = stepType.NewRow();
type[0] = 2;
type[1] = "type2";
stepType.Rows.Add(type);
comboBoxCell.ValueMember = "Value";
comboBoxCell.DisplayMember = "Name";
comboBoxCell.DataSource = stepType;
}