DataGridView 添加ComboBox控件

DataGridView 添加ComboBox 

第一:
先在窗体设计时拖一个ComBoBox控件,然后在里面的ITEMS设好你要下拉项,这个不用教了吧...

第二:
在窗体的Load方法中加入:g_DataGridView.Controls.Add(g_ComBoBox);也就是把ComBoBox控件添加到DataGridView控件中

第三:
在DataGridView控件的CurrentCellChanged方法中写如下代码:

DataGridViewCell CurrnetCell = g_View.CurrentCell;
if (CurrnetCell != null && CurrnetCell.OwningColumn.Name == "列名")
{
    Rectangle TmpRect = g_DataGridView.GetCellDisplayRectangle(CurrnetCell.ColumnIndex, CurrnetCell.RowIndex, true);
    g_ComBoBox.Text = CurrnetCell.Value.ToString();
    g_ComBoBox.Size = TmpRect.Size;
    g_ComBoBox.Top = TmpRect.Top;
    g_ComBoBox.Left = TmpRect.Left;
    g_ComBoBox.Visible = true;
}
else
{
    g_ComBoBox.Visible = false;
}

 

最后在ComBoBox控件的SelectedIndexChanged方法中写:

g_DataGridView.CurrentCell.Value = g_ComBoBox.Text; 

 



添加一个 
DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();列后 
可以设置: 
column1 .DisplayMember = 显示的字段 
column1 .ValueMember = 值 
column1 .DataSource=数据源 

 

转 : https://www.cnblogs.com/yelaiju/archive/2010/10/05/1842920.html

 

posted @ 2022-04-13 14:06  与f  阅读(912)  评论(0编辑  收藏  举报