datagridview的combobox控件的添加
//以下代码是从网上借鉴,并修改的,忘了那个人叫什么了。。不好意思。
private int Combobox_Index_Num = -1;//datatgridview里需要添加的列是第几列
private string Combobx_DSource_Name = "";//添加列的名字
private ComboBox DataGridViewCombobox;//定义一个Combobox
public void DataSourceBing()
{
Combobx_DSource_Name = "设备";//设备列变成combobox列
Combobox_Index_Num = 3;
DataGridViewCombobox = new ComboBox();
DataGridViewCombobox.DropDownStyle = ComboBoxStyle.DropDownList;//定义Combobox的下拉类型
DataGridViewCombobox.Visible = false;
DataGridViewCombobox.SelectedIndexChanged += DataGridViewCombobox_SelectedIndexChanged;//委托Combobox的selectIndexChanged事件
dataGridView6.DataSource = null;
dataGridView6.AutoGenerateColumns = false;//不允许绑定数据库时自动添加列
dataGridView6.ColumnCount = 3;//定义datatgridview的列数
dataGridView6.DataSource = _zhdt;//绑定数据库
dataGridView6.Columns[0].HeaderText = "名称";//列的名字
dataGridView6.Columns[0].DataPropertyName = "Name";//绑定的数据库中的字段
dataGridView6.Columns[1].HeaderText = "类别";
dataGridView6.Columns[1].DataPropertyName = "Lock";
dataGridView6.Columns[2].HeaderText = "编码";
dataGridView6.Columns[2].DataPropertyName = "Code";
dataGridView6.Columns[3].HeaderText = "设备";
dataGridView6.Columns[3].DataPropertyName = "Device";
dataGridView6.Controls.Add(DataGridViewCombobox);//把combobox添加到datatgridview里
}
/// <summary>
/// DataGridViewCombobox的选着事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void DataGridViewCombobox_SelectedIndexChanged(object sender, EventArgs e)
{
if (null != DataGridViewCombobox.SelectedItem)
{
if (!ToStr(dataGridView6.CurrentCell.Value).Equals(DataGridViewCombobox.SelectedItem))
{
dataGridView6.CurrentCell.Selected = true;
dataGridView6.CurrentCell.Value = DataGridViewCombobox.SelectedItem;//就是把combobox的值显示到当前列
if (dataGridView6.CurrentRow.IsNewRow)
{
//执行这两句代码,可以让一行进入编辑状态,不再是NewRow
dataGridView6.NotifyCurrentCellDirty(true);
dataGridView6.NotifyCurrentCellDirty(false);
}
}
}
}
/// <summary>
/// 绑定DataGridViewCombobox的值
/// </summary>
private void ResetCompanyCombo()
{
DataGridViewCombobox.Items.Clear();
DataGridViewCombobox.Items.Add("大锁");
DataGridViewCombobox.Items.Add("小锁");
}
/// <summary>
/// 如果对象是null返回“”
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public string ToStr(Object obj)
{
if (null == obj)
{
return "";
}
else
{
return obj.ToString();
}
}
private void dataGridView6_CellClick(object sender, DataGridViewCellEventArgs e)
{
//判断datagridview的cellclick是不是绑定的combobox列
if (e.RowIndex > -1 && e.ColumnIndex > -1 && e.ColumnIndex == Combobox_Index_Num)
{
dataGridView6_CurrentCellChanged(null, null);//如果是调用这个事件
}
else //如果不是。绑定的combobox不显示。还是显示datagridview的cell
{
DataGridViewCombobox.Visible = false;
}
}
private void dataGridView6_CurrentCellChanged(object sender, EventArgs e)
{
DataGridViewCell CurrnetCell = dataGridView6.CurrentCell;
if (CurrnetCell != null)
{
//先把ComboBox隐藏,并回复Grid的EditMode
DataGridViewCombobox.Visible = false;
if (Combobox_Index_Num == CurrnetCell.ColumnIndex)
{
//重设下拉列表
ResetCompanyCombo();//绑定Combobox的下拉items
DataGridViewCombobox.SelectedIndex = -1;
DataGridViewCombobox.SelectedIndexChanged +=
new EventHandler(DataGridViewCombobox_SelectedIndexChanged);
}
else
{
DataGridViewCombobox.Visible = false;
dataGridView6.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
return;
}
//设置ComboBox的显示位置。。主要是这个
Rectangle TmpRect = dataGridView6.GetCellDisplayRectangle(CurrnetCell.ColumnIndex,
CurrnetCell.RowIndex, true);
if (CurrnetCell.Value.ToString().Length != 0)
{
DataGridViewCombobox.SelectedValue = ToStr(CurrnetCell.Value);
}
DataGridViewCombobox.Size = TmpRect.Size;
DataGridViewCombobox.Top = TmpRect.Top;
DataGridViewCombobox.Left = TmpRect.Left;
//显示ComboBox,并打开下拉列表
DataGridViewCombobox.Visible = true;
if (CurrnetCell.Selected)
{
DataGridViewCombobox.DroppedDown = true;
}
//ComboBox显示的时候,为了让该Cell不能直接输入值,要改变Grid的EditMode
dataGridView6.EditMode = DataGridViewEditMode.EditProgrammatically;
}
else
{
//没有选中任何Cell的时候,把ComboBox隐藏,并回复Grid的EditMode
DataGridViewCombobox.Visible = false;
dataGridView6.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
}
}
}
}
大体是这样做的。大家可以修改。还有其他方法添加的Combobox