【问题标题】:How can I prevent users from entering data into DataGridViewComboBoxCells?如何防止用户将数据输入 DataGridView ComboBox?
【发布时间】:2011-05-28 07:52:56
【问题描述】:

我有一个使用许多 DataGridView 控件的项目。大多数单元格都是DataGridViewTextBoxCell 的说服。我这样声明我的控件:

Dim MyCell as DataGridViewCell

稍后我会指定它们是 DataGridViewTextBoxControls 还是 DataGridViewComboBoxCells,如下所示:

MyCell = New DataGridViewTextBoxCell   ...or...
MyCell = New DataGridViewComboBoxCell

我的代码中的任何地方都不需要用户在组合框中输入自己的值。也就是说,它们要么是硬编码的,要么是从应用程序中的其他数据收集的值。

目前,用户可以突出显示如上所示定义的组合框控件并开始输入他们喜欢的任何内容。例如,我有一个组合框,它为用户提供 1 到 9 之间的整数选择。我可以突出显示组合框单元格并输入“Hello, World!”。如果我愿意的话。

如何禁用此功能?我确定有一处房产,但我还没有找到它。我在 Internet 上进行了搜索,发现只有将用户输入的值添加到列表和数据绑定教程流中的功能。

提前感谢您! C

【问题讨论】:

    标签: .net vb.net datagridviewcomboboxcell


    【解决方案1】:

    查看here

    编辑

    我可能应该提供更多信息,但我累了,感觉有点恶心,所以我想偷懒……但不,你必须破坏我的懒惰并强迫我多打字。 (哈哈,开个玩笑)。

    本质上,您在组合框单元中看到的组合框实际上是一个组合框控件(疲倦 = 英语不好)。

    据我所知,您应该能够通过使用 datagridview 的 EditingControlShowing 事件来获取单元格中使用的控件。在那里,您只需设置下拉样式。

    喜欢:

    Private Sub datagridview_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
    
        Dim c As ComboBox = e.Control
    
        If Not c Is Nothing Then
    
            c.DropDownStyle = ComboBoxStyle.DropDownList
    
        End If
    
    End Sub
    

    【讨论】:

    • 如果这是一个直接的 ComboBox 控件,而不是 DataGridViewComboBoxCell,则可以正常工作。除非 DataGridViewComboBoxCells 有一个神奇的技巧允许他们使用这个属性,而我还没有找到它。
    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多