【问题标题】:Adding ComboBox to Datagridview将组合框添加到 Datagridview
【发布时间】:2014-09-11 21:21:16
【问题描述】:

我找到了一种将组合框添加到 DataGridview (Winform) 单元格的方法,但是我没有找到像 DataGridView 的 ItemDataBound 这样的事件来为组合框设置值。并且不知道如何将组合框的选定值设置为当前行(DataGridView)的 DataItem 属性:(

请给我一些线索来完成这项任务

非常感谢

【问题讨论】:

  • 你试过了吗?
  • 你想在datagridview中添加组合框
  • @DhavalPatel 是的,我做到了,但没有运气
  • @Sathish 是的,我是认真的。我想在 DataGridView 的每一行都有一个组合框。我想在项目数据绑定事件时设置组合框值(没有找到这个内置事件或类似事件,请帮助),并且每次用户选择组合框的项目时,所选项目将分配给数据项属性
  • 为组合框设置哪个值?

标签: c# winforms datagridview datagridviewcombobox rowdatabound


【解决方案1】:

您可以使用以下方法将数据添加到 gridview 中的组合框。如果您没有列表,您可以将项目添加到组合框中:

cmbdgv.Items.Add("Test");

private void bindDataToDataGridViewCombo() {
    DataGridViewComboBoxColumn cmbdgv = new DataGridViewComboBoxColumn();
    List<String> itemCodeList = new List<String>();
    cmbdgv.DataSource = itemCodeList;
    cmbdgv.HeaderText = "Test";
    cmbdgv.Name = "Test";
    cmbdgv.Width = 270;
    cmbdgv.Columns.Add(dgvCmbForums);
    cmbdgv.Columns["Test"].DisplayIndex = 0;
}

添加后,如果要捕获组合框选择更改,可以在 datagridview 中使用以下事件。

ComboBox cbm;
DataGridViewCell currentCell;

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (e.Control is ComboBox)
    {
        cbm = (ComboBox)e.Control;

        if (cbm != null)
        {
            cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
        }

        currentCell = this.dataGridView1.CurrentCell;
    }
}

void cbm_SelectedIndexChanged(object sender, EventArgs e)
{
    this.BeginInvoke(new MethodInvoker(EndEdit));
}


void EndEdit()
{
    if (cbm != null)
    {
        string SelectedItem=cbm.SelectedItem.ToString();
        int i = dataGridView1.CurrentRow.Index;
        dataGridView1.Rows[i].Cells["Test"].Value = SelectedItem;
    }
}

【讨论】:

  • 是否需要添加if (cbm != null)条件?
【解决方案2】:

如果您尝试将值设置为 DataGridView 中的 Combobox,请查看this 答案是否会有所帮助。

获取Combobox的选中项(示例):

comboBox.SelectedIndexChanged += new EventHandler(comboBox_ComboSelectionChanged);

private void comboBox_ComboSelectionChanged(object sender, EventArgs e)
      {
            if (myDGV.CurrentCell.ColumnIndex == 5)
            {
                int selectedIndex;
                string selectedItem;

                selectedIndex = ((ComboBox)sender).SelectedIndex;  // handle an error here.
                // get the selected item from the combobox
                var combo = sender as ComboBox;

                if (selectedIndex == -1)
                {
                    MessageBox.Show("No value has been selected");
                }
                else
                {
                    // note that SelectedItem may be null
                    selectedItem = combo.SelectedItem.ToString();

                    if (selectedItem != null)
                    {
                        // Your code

【讨论】:

  • 我的意思是,DataGridView 的每一行都有一个组合框
猜你喜欢
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多