使DataGridViewComboBoxColumn的Comb

使DataGridViewComboBoxColumn的Comb

本文介绍了如何使DataGridViewComboBoxColumn的ComboBox接受用户新项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DataGridViewComboBoxColumn ,目标是使组合框接受用户的新项目,也可以选择已存在的项目。

Using a DataGridViewComboBoxColumn, the goal is to make the ComboBoxes accept user's new items and also the possibility of choosing items that are already present.

我知道使用 DataGridView EditingControlShowing 事件更改 DropDownStyle DataGridViewComboBoxEditingControl 在运行时允许这个,但我想知道是否可以在较低的级别完成。

I'm aware of using EditingControlShowing event of the DataGridView to change the DropDownStyle of the DataGridViewComboBoxEditingControl at run time to allow this, but I'm wondering if this can be done at a lower level.

我现在正在做的是扩展 DataGridViewComboBoxColumn DataGridViewComboBoxCell DataGridViewComboBoxEditingControl ,希望在实例化之前更改 EditingControl的DropDownStyle 。到目前为止,没有运气。

What I'm doing now is extending DataGridViewComboBoxColumn, DataGridViewComboBoxCell and DataGridViewComboBoxEditingControl, hoping to change the EditingControl's DropDownStyle in the moment I instantiate it. So far, no luck.

调试器显示正在执行的正确分配,但是仍然出现了 DropDownStyle EditingControlShowing (使用事件进行调试)作为DropBoxList ,而不是DropBox ,这是意图。

The debugger shows the right assignment is being executed, but nonetheless, the DropDownStyle is popping at the EditingControlShowing (using the event for debugging purposes) as DropBoxList, not DropBox, which is the intent.

这里按照以下类:

Public Class DataGridViewComboBoxColumnALT
    Inherits System.Windows.Forms.DataGridViewComboBoxColumn

    Public Sub New()
        Me.CellTemplate = New DataGridViewComboBoxCellALT
    End Sub
End Class

Public Class DataGridViewComboBoxCellALT
    Inherits System.Windows.Forms.DataGridViewComboBoxCell
    Public Overrides ReadOnly Property EditType() As Type
        Get
            Return GetType(DataGridViewComboBoxEditingControlALT)
        End Get
    End Property
End Class

Public Class DataGridViewComboBoxEditingControlALT
    Inherits System.Windows.Forms.DataGridViewComboBoxEditingControl
    Implements System.Windows.Forms.IDataGridViewEditingControl
    Public Sub New()
        MyBase.New()
        Me.DropDownStyle = ComboBoxStyle.DropBox
    End Sub
End Class


推荐答案

您需要设置DataGridView.EditingControl的DropDownStyle Inherted单元格类的初始化编辑控件方法

You need to set DropDownStyle of DataGridView.EditingControl @ InitializeEditingControl method of Inherted cell class

class DataGridViewComboBoxCellExtended : DataGridViewComboBoxCell
{
    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        ((DataGridViewComboBoxEditingControlExtended)DataGridView.EditingControl).DropDownStyle = ComboBoxStyle.DropDown;
    }
    public override Type EditType
    {
        get
        {
            return typeof(DataGridViewComboBoxEditingControlExtended);
        }
    }

这篇关于如何使DataGridViewComboBoxColumn的ComboBox接受用户新项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:42