首页 文章

WinForms:DataGridView - 当单元格被编辑时显示组合框

提问于
浏览
0

在C#应用程序中,我有一个DatagridView,所有单元格都是DataGridTextBoxCol . 使用未绑定的值动态填充网格 . 网格有不同类型的食物 . 我的需求是,当用户点击/想要编辑一个单元格时 - 一个带有食物项目列表的组合框出现在单元格的位置 . 如果它们是单元格中的任何值,则应在组合中选择该值 . 用户可以在组合中键入和选择项目 . 这是我到目前为止所尝试的:

private void PopulateAllergensCombo()
    {
        // Populate Combo box in Form_load & be hidden
        BindingSource allergensBindSource = new BindingSource();
        allergensList = dbOperations.GetAllergensListObjects();

        allergensBindSource.DataSource = allergensList;

        allergensCmb.DataSource = allergensBindSource.DataSource;   // allergensList;
        allergensCmb.DisplayMember = "Name";
        allergensCmb.ValueMember = "AllergensNumber";
    }

将此组合 allergensCmb 放在当前单元格中

private void cellAssignments_dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        DataGridViewCell dvCell = cellAssignments_dgv.CurrentCell;

        allergensCmb.Location = dvCell.ContentBounds.Location; // location of cell is X=0, Y=11 - This seems to be relevant from grid & not from Form

        if ( String.IsNullOrEmpty(cellAssignments_dgv.CurrentCell.Value.ToString()) == false )
            allergensCmb.SelectedValue = cellAssignments_dgv.CurrentCell.Value;
        allergensCmb.Show();
    }

与上面的 dgv_CellBeginEdit 事件代码一样,组合与表单维度无关't show in the cell location. Location of the cell is X=0, Y=11 - 1st col 1st row cell. It' . 为了获取和设置从网格单元格中的组合中选择的项目的值,我尝试实现CellEndEdit和allergensCmb_SelectedIndexChanged事件 . 但没有什么能100%完成 . 使用selectedIndexChanged事件时,由于单元格不再活动,我丢失了当前单元格;不能得到当前的细胞或它是否脏!

任何线索,我如何实现这种类型的控件 - 在DataGridView的当前单元格上显示一个Combobox . 我做了很多研究并尝试过,但没有任何工作按预期进行 .

任何帮助都非常感谢 .

////////////////////////////////////////// ******** **** UPDATIONS //////////////////////////////////////////

你们都建议使用DataGridViewComboBoxColumn,我做了:

// Add Columns
        for (int i = 1; i <= pair.Value.CellBodyRowData.Count; i++)
        {
            DataGridViewComboBoxColumn cbCol = new DataGridViewComboBoxColumn();
            cbCol.HeaderText = i.ToString();
            cbCol.Name = i.ToString();

            cbCol.DataSource = allergensList;
            cbCol.ValueMember = "AllergensNumber";
            cbCol.DisplayMember = "Name";

            cellAssignments_dgv.Columns.Add(cbCol);

            // *** cellAssignments_dgv.Columns.Add((i.ToString(), i.ToString());
        }

// Populate in each Row
foreach (CellBodyRowData cbrData in cbRow.CellBodyRowData)
                {
                    // *****  dgvr.Cells[cbrData.Col - 1].Value = cbrData.CellAllergen.Name;

                    if (cbrData.CellAllergen.AllergensNumber > 0)
                        dgvr.Cells[cbrData.Col - 1].Value = cbrData.CellAllergen.AllergensNumber;
                    else
                        dgvr.Cells[cbrData.Col - 1].Value = 0;

                }

这可以 . 在网格中,我将每个单元格视为仅限ComboBox . 如何使其仅显示为普通文本,并且仅在编辑时显示为组合框 . 使用EditingControlShowing事件的原因是什么 - 我没有得到那个逻辑?我需要更改值并将标志 isCellAssignGridChanged 设置为true,因此可以更新数据库 .

你能不能对这一部分有所了解 .

谢谢

1 回答

  • 0

    谢谢@Plutonix和@JohnKane .

    • 我在网格中将cols更改为 DataGridViewComboBoxColumn .

    • 更改列的 DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing; .

    • 已实施 EditingControlShowing 事件并更改了Combobox的下拉样式 cb.DropDownStyle = ComboBoxStyle.DropDownList; 和SelectionChangeCommitted

    private void cellAssignments_dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ComboBox cb = (ComboBox)e.Control;
            if (cb != null)
            {
                // Show the DropDown of the combo & set its event
                cb.DropDownStyle = ComboBoxStyle.DropDownList;
    
                cb.SelectionChangeCommitted -= cb_SelectionChangeCommitted;
                cb.SelectionChangeCommitted += cb_SelectionChangeCommitted;
            }
        }
    }
    
    void cb_SelectionChangeCommitted(object sender, EventArgs e)
    {
        ComboBox cb = (ComboBox)sender;
    
        if (cb != null)
        {
            Console.WriteLine("Selected Combo = " + cb.SelectedText + " Value = " + cb.SelectedValue);
    
            // Notify the cell is dirty
            cellAssignments_dgv.NotifyCurrentCellDirty(true);
            // Force to End Edit the Cell
            cellAssignments_dgv.EndEdit();
        }
    
    }
    
    • 最后,在 CellEndEdit 事件中,实现了我的更新更新集合对象

    希望这对某人有所帮助 .

    问候,

相关问题