一:DatagridviewComboBox 选定索引更改时更改 DatagridviewTextBox 文本内容
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1 && e.Control is ComboBox)
{
ComboBox comboBox = e.Control as ComboBox;
comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
}
}
private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
var currentcell = dataGridView1.CurrentCellAddress;
var sendingCB = sender as DataGridViewComboBoxEditingControl;
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridView1.Rows[currentcell.Y].Cells[0];
cel.Value = sendingCB.EditingControlFormattedValue.ToString();
}
二。添加DatagridviewComboBox下拉组合框内容的二种方式
//方式一
((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("A" );
((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("B");
((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).Items.Add("C" );
//方式二
List<string> ListData = new List<string> { "A", "B", "C" };
((DataGridViewComboBoxCell)this.Section_data.Rows[index1].Cells[2]).DataSource = ListData;
三。组合框内容的显示
//1要设置显示类型 2要设置的值为组合列表内的元素
this.Section.ValueType = typeof(string);
this.Section_data.Rows[index1].Cells[2].Value = "B";