c# - DataGridViewComboBox列: limit MaxDropDownItems

标签 c# combobox datagridviewcombobox

我想知道如何限制 DataGridViewComboBoxColumn 中显示的项目数量。 在简单的 ComboBox 中,我们可以这样做:

comboBox1.IntegralHeight = false; //this is necessary to make it work!!!
comboBox1.MaxDropDownItems = 3;

但是如何在 DGV 的组合框`中执行相同的操作?

创建ComboBoxColumn时,没有IntegralHeight属性。

最佳答案

我自己通过订阅 DataGridViewEditControlShowing 事件并在其中添加了以下代码来解决这个问题:

private void dataGridView1_EditingControlShowing(
    object sender, 
    DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox cb = e.Control as ComboBox;
    if (cb != null)
    {
        cb.IntegralHeight = false;
        cb.MaxDropDownItems = 10;
    }
}

现在下拉菜单可以正常工作,它会显示为 MaxDropDownItems 属性设置的行数。

对于 Visual Basic

Private Sub dgvDesp_EditingControlShowing( _
    sender As Object, _
    e As DataGridViewEditingControlShowingEventArgs) Handles dgvDesp.EditingControlShowing
    Dim cb As ComboBox = e.Control
    If cb.Items.Count > 0 Then
        cb.IntegralHeight = False
        cb.MaxDropDownItems = 10
    End If
End Sub

关于c# - DataGridViewComboBox列: limit MaxDropDownItems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23155293/

相关文章:

c# - 无法使用 vb.net 在 asp.net mvc 3 中的区域中加载 View

c# - 将自定义列表与组合框等控件连接起来

vb.net - 如何更改ComboBox的数据源?

c# - 使用 ComboBox.SelectedItem = -1 时 ComboBox 不加载空白值

c# - 建议使用什么来监控我的 asp.net 应用程序的流量

c# - MVVM 将嵌套 subview 连接到 subview 模型

c# - 启动时运行后台任务

c# - datagridview 组合框列表颜色更改为黑色 c#

vb.net - 创建动态 DataGridViewComboBoxCell