2008-10-27 90 views
18

我在我的WinForms应用程序中有一个DataGridViewComboBoxColumn的DataGridView。我需要手动下拉(打开)这个DataGridViewComboBoxColumn,比方说,点击一个按钮后。如何手动下拉DataGridViewComboBoxColumn?

我需要这个的原因是我已经将SelectionMode设置为FullRowSelect,我需要点击2-3次打开组合框。我想单击组合框单元,它应该立即下降。我想用CellClick事件做到这一点,或者有其他方法吗?

我在谷歌和VS帮助搜索,但我还没有找到任何信息。

任何人都可以帮忙吗?

回答

22

我知道这不是理想的解决方案,但它确实创建了单元格内的单击组合框。

Private Sub cell_Click(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
     DataGridView1.BeginEdit(True) 
     If DataGridView1.Rows(e.RowIndex).Cells(ddl.Name).Selected = True Then 
      DirectCast(DataGridView1.EditingControl, DataGridViewComboBoxEditingControl).DroppedDown = True 
     End If 
    End Sub 

其中“ddl”是我在gridview中添加的组合框单元格。

10

我已经能够你在找什么亲近通过设置

DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter 

只要没有其他单元格的下拉列表中。它应该立即显示选定的单元格的下拉列表。

如果有什么问题,我会不断思考和更新。

15

谢谢ThisMat,您的解决方案完美地工作。

我在C#代码:

private void dataGridViewWeighings_CellClick(object sender, DataGridViewCellEventArgs e) { 
    if (e.RowIndex < 0) { 
     return;  // Header 
    } 
    if (e.ColumnIndex != 5) { 
     return;  // Filter out other columns 
    } 

    dataGridViewWeighings.BeginEdit(true); 
    ComboBox comboBox = (ComboBox)dataGridViewWeighings.EditingControl; 
    comboBox.DroppedDown = true; 
} 
+0

我很高兴你得到它的工作! – thismat 2008-10-28 12:41:38

2

感谢C#版本。以下是我对组合列名搜索的贡献:

private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) 
{ 
    string Weekdays = @"MondayTuesdayWednesdayThursdayFridaySaturdaySunday"; 
    if (Weekdays.IndexOf(dgv.Columns[e.ColumnIndex].Name) != -1) 
    { 
     dgv.BeginEdit(true); 
     ComboBox comboBox = (ComboBox)dgv.EditingControl; 
     comboBox.DroppedDown = true; 
    } 
} 
1

我正在寻找这个答案。我最终编写了一个可以从任何DataGridView中调用的泛型子类,因为我的应用程序中有很多东西,我希望它们都以相同的方式运行。这对我来说非常合适,所以我想与任何偶然发现这篇文章的人分享。

在鼠标点击事件的DGV我添加代码

Private Sub SomeGrid_MouseClick(sender As Object, e As MouseEventArgs) Handles SomeGrid.MouseClick 
    DGV_MouseClick(sender, e) 
End Sub 

它调用下面子,我存储共享模块

Public Sub DGV_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 
    Try 
     Dim dgv As DataGridView = sender 
     Dim h As DataGridView.HitTestInfo = dgv.HitTest(e.X, e.Y) 
     If h.RowIndex > -1 AndAlso h.ColumnIndex > -1 AndAlso dgv.Columns(h.ColumnIndex).CellType Is GetType(DataGridViewComboBoxCell) Then 
      Dim cell As DataGridViewComboBoxCell = dgv.Rows(h.RowIndex).Cells(h.ColumnIndex) 
      If Not dgv.CurrentCell Is cell Then dgv.CurrentCell = cell 
      If Not dgv.IsCurrentCellInEditMode Then 
       dgv.BeginEdit(True) 
       CType(dgv.EditingControl, ComboBox).DroppedDown = True 
      End If 
     End If 
    Catch ex As Exception 
    End Try 
End Sub 

我没有抓到任何错误的,我只包括Try..Catch代码为一些罕见的例子,我想不出可能会抛出异常。我不希望用户因为此场景的错误消息而烦恼。如果分组失败,那么DGV很可能就像通常那样运行。