DataGridViewComboBoxColumn: limit MaxDropDownItems

本文关键字:MaxDropDownItems limit DataGridViewComboBoxColumn | 更新日期: 2023-09-27 17:59:04

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

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

但是如何在DGV的comboBox`中执行同样的操作?

创建ComboBoxColumn时,没有IntegralHeight属性。

DataGridViewComboBoxColumn: limit MaxDropDownItems

我自己通过订阅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
相关文章:
  • 没有找到相关文章