第一种:
下拉框动态赋值:
private void Form1_Load(object sender, EventArgs e)
{
//获取数据源list
this.Column1.DataSource = list;
this.Column1.DisplayMember = "";
this.Column1.ValueMember = "";
}
private DataGridViewComboBoxEditingControl dataGridViewComboBox = null;
//捕获事件
private void dgvTableFields_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl && this.dgvTableFields.CurrentCell.ColumnIndex ==4
&& this.dgvTableFields.CurrentCell.RowIndex !=-1)
{
this.dataGridViewComboBox = (DataGridViewComboBoxEditingControl)e.Control;
//增加委托处理
dataGridViewComboBox.SelectionChangeCommitted += new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
}
}
private void dataGridViewComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
//处理业务
MessageBox.Show(((ComboBox)sender).Text);
}
private void dgvTableFields_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridViewComboBox != null)
{
dataGridViewComboBox.SelectionChangeCommitted -= new EventHandler(this.dataGridViewComboBox_SelectionChangeCommitted);
this.dataGridViewComboBox =null;
}
}
第二种:
在DataGridView控件中加入ComboBox下拉列表框的实现