DataGridViewComboBoxColumn: limit MaxDropDownItems

不想你离开。 提交于 2019-12-02 03:27:25

问题


I would like to know how to limit the number of items to show in DataGridViewComboBoxColumn. In simple ComboBox we can do it as:

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

But how to do the same in DGVs comboBox`?

When creating ComboBoxColumn there is no IntegralHeight property.


回答1:


I have figured it out by my self, by subscribing to DataGridViewEditControlShowing event, and with this code inside of it:

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

Now the dropdown menu works, it shows as many rows as set for the MaxDropDownItems property.

For 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


来源:https://stackoverflow.com/questions/23155293/datagridviewcomboboxcolumn-limit-maxdropdownitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!