DataGridView使用ComboBox替换Cell

Cell中弹出ComboBox

在DataGridView添加CellClick事件。当点击某个Cell时,在Cell上弹出ComboBox。ComboBox选中后在Cell中显示选中值,同时关闭ComboBox。

主要事件

DataGridView_CellClick

private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int rowNum = e.RowIndex;
    int colNum = e.ColumnIndex;

    if (rowNum < 0)
    {
        return; // 点击标题时,row = -1 返回不做处理
    }
    if (colNum == 0)
    {
        string value = "";
        object cellValue = DataGridView1.Rows[rowNum].Cells[colNum].Value;
        if(cellValue != null)
        {
            value = cellValue.ToString();
        }
        leftCobmo.SelectedValue = value;
        leftCobmo.Name = "DataGridView1_cell_" + rowNum + "_" + colNum;
        leftCobmo.SelectedIndexChanged += GridCobmo_SelectedIndexChanged;
        Add_Control(DataGridView1, leftCobmo, rowNum, colNum);
    }

}

 leftCombo配置

public MyForm()
{
    InitializeComponent();
    this.initConditionList();
    this.InitCombo();
}

private void initConditionList()
{
    conditionList.Add(new KeyValuePair<string, string>("1", "1"));
    conditionList.Add(new KeyValuePair<string, string>("2", "2"));
}

private void InitCombo()
{
    leftCobmo.DataSource = conditionList;
    leftCobmo.DisplayMember = "value";
    leftCobmo.ValueMember = "key";
    leftCobmo.DropDownStyle = UIDropDownStyle.DropDownList;
}

在此处leftCombo的DataSource是固定列表。

如果conditionList是动态的,比如从数据库临时查询得到,那么需要在查询后再绑定DataSource。

因为ComboBox一旦设置了DataSource后,DataSource不可改变。

这里的不可改变,指的是,当conditionList发生改变后,leftCombo的选项不变。

如果想要改变leftCombo的数据源,则:

leftCombo.DataSource = null;
leftCombo.Items.Clear();

然后再设置 DataSource。 

ComboBox_SelectedIndexChanged

在弹出ComboBox后,为ComboBox绑定SelectedIndexChanged事件。在事件中,把选中值更新到相应的Cell中。

private void GridCobmo_SelectedIndexChanged(object sender, EventArgs e)
{ 
    ComboBox combo = (ComboBox)sender;
    string name = combo.Name; // 格式为 gridname_cell_rowIndex_colIndex
    string[] arr = name.Split("_");
    string gridName = arr[0];
    int row = int.Parse(arr[2]);
    int col = int.Parse(arr[3]);
    Control[] grids = this.Controls.Find(gridName, true); // 查找到 DataGridView
    if (grids != null && grids.Length > 0)
    {
        DataGridView grid = (DataGridView)grids[0];
        string value = combo.SelectedValue.ToString(); // 获取combobox选中值
        grid.EndEdit(); // 在编辑状态下,无法对cell进行赋值。先结束编辑状态再赋值。
        grid.Rows[row].Cells[col].Value = combo.SelectedValue;
        grid.Controls.Remove(combo); // 从DataGridView中删除ComboBox
    }
    combo.SelectedIndexChanged -= GridCobmo_SelectedIndexChanged; // 去除SelectedIndexChanged事件
}

Add_Control方法

把弹出的ComboBox显示在DataGridView的相应位置上。

public void Add_Control(DataGridView grid, Control ctrl, int row_index, int col_index)
{
    grid.Controls.Add(ctrl);
    ReLocation(grid, ctrl, row_index, col_index);
    Rectangle rectangle = grid.GetCellDisplayRectangle(col_index, row_index, true);
    ctrl.Size = new Size(rectangle.Width, rectangle.Height - 2);
    ctrl.Location = new Point(rectangle.Left, rectangle.Top);
}

以上,即全部主要代码。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm中的DataGridView控件是用于显示和编辑数据的强大工具。要在DataGridView中添加ComboBox控件,可以按照以下步骤进行操作: 1. 首先,在设计模式下打开窗体,在工具箱中找到DataGridView控件并将其拖放到窗体上。 2. 在DataGridView的列标题栏上右键单击,选择“添加列”选项。在弹出的对话框中,选择“DataGridViewComboBoxColumn”作为列类型,并点击“确定”按钮。 3. 在DataGridView的列属性中,您可以设置列的HeaderText(列标题文本)、Name(列的名称)和DataPropertyName(列绑定的数据属性名称)等属性。 4. 在代码中,您可以通过以下代码为ComboBox列添加数据项: ``` DataGridViewComboBoxColumn comboBoxColumn = (DataGridViewComboBoxColumn)dataGridView1.Columns["columnName"]; comboBoxColumn.Items.Add("Item 1"); comboBoxColumn.Items.Add("Item 2"); comboBoxColumn.Items.Add("Item 3"); ``` 5. 您还可以通过以下代码为每个单元格提供不同的ComboBox选项: ``` private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == columnIndex) // 根据需要更改要添加ComboBox的列的索引 { DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell(); comboBoxCell.Items.Add("Item 1"); comboBoxCell.Items.Add("Item 2"); comboBoxCell.Items.Add("Item 3"); dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] = comboBoxCell; } } ``` 通过以上步骤,您可以成功在WinForm的DataGridView中添加ComboBox控件,并设置ComboBox的数据项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值