I wanted to create a multi-column combobox, so I decided to try to use another DataGridView as the dropdown.
I have this somewhat working... I have a few issues however and I need direction. (Keep in mind, I have only been doing
VB.Net for a couple of months)
I created the Column, Cell, and EditingControl, and I inherited the combobox column, cell, and editingcontrol for each one, respectively. I simply pass the DataSource down to the EditingControl, create a datagridview, set the datasource, and then add it to the controls collection, and then show it. This works... I can see the data in the grid, the columns, etc. I have a few problems...
1. If I add the grid to the EditingControl controls collection, the grid is in that editcontrol, so it will only show up inside the editcontrol. This would be fine if it showed up in the dropdown list, but it doesn't, it only shows up in the cell area.
2. If I add the grid to the form's controls collection, it works, except if you click in the grid, the dropdown closes and the editingcontrol disappears and so does the grid (if you remove the grid in the dropdownclosed event).
3. The location of the grid is a problem... I can't seem to find a location property that will tell the grid where to pop up below the cell you are dropping down. Plus, if the grid is part of the Form, it will only show up in the form, if the parent grid is at the bottom of the form, the dropdown grid will probably not show part of it because it will go below where the form stops...
Any idea how to overcome these? Is there a way to get the drop down window and have the dropdown grid display in there instead? That would be the ideal solution. If the drop down is open and you change items, the current row of the drop down grid changes, so I am assuming since they are the same datasource, if you change it in the grid, the dropdown will change as well..
The code so far is below if you need to play with it:
Public Class DataGridViewDropGridColumn
Inherits DataGridViewComboBoxColumn
Dim DropGridCell As DataGridViewDropGridCell
Public Overrides Function Clone() As Object
Dim newObj As DataGridViewDropGridColumn = MyBase.Clone()
Return newObj
End Function
Public Sub New()
DropGridCell = New DataGridViewDropGridCell()
MyBase.CellTemplate = DropGridCell
End Sub
End Class
Public Class DataGridViewDropGridCell
Inherits DataGridViewComboBoxCell
Public Overrides Function Clone() As Object
Dim newObj As DataGridViewDropGridCell = MyBase.Clone()
Return newObj
End Function
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As DataGridViewDropGridEditingControl = CType(DataGridView.EditingControl, DataGridViewDropGridEditingControl)
ctl.GridView.DataSource = Me.DataSource
ctl.Text = CType(Me.Value, String)
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(DataGridViewDropGridEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(Decimal)
End Get
End Property
End Class
Public Class DataGridViewDropGridEditingControl
Inherits DataGridViewTextBoxEditingControl
Dim grid As DataGridView
Property GridView() As DataGridView
Get
Return grid
End Get
Set(ByVal value As DataGridView)
grid = value
End Set
End Property
Private Sub DataGridViewDropGridEditingControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
End Sub
Private Sub DataGridViewDropGridEditingControl_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles Me.DropDown
Dim iIndex As Integer
Dim frmParent As Form
frmParent = Me.EditingControlDataGridView.GetContainerControl( )
grid.Left = Me.Parent.Left
grid.Top = Me.Parent.Top
frmParent.Controls.Add(grid)
iIndex = frmParent.Controls.GetChildIndex(grid)
frmParent.Controls(iIndex).BringToFront()
grid.Show()
End Sub
Private Sub DataGridViewDropGridEditingControl_DropDownClosed( ByVal sender As Object, ByVal e As System.EventArgs) 'Handles Me.DropDownClosed
Dim frmParent As Form
frmParent = Me.EditingControlDataGridView.GetContainerControl( )
grid.Hide()
frmParent.Controls.Remove(grid)
End Sub
Public Sub New()
grid.AutoGenerateColumns = True
End Sub
End Class