Here's some code on MSDN (I Didn't write it) for creating a
DataGridComboBo xColumn and DataGridComboBo x. Since the DataGridComboBo x
inherits ComboBox, you can still bind it and if you create two different
columns, you can add values to one statically and bind the other, bind them
to the same source, whatever you please:
#Region "DataGridComboB ox"
Public Class DataGridComboBo x
Inherits ComboBox
Private WM_KEYUP As Integer = &H101
Protected Overrides Sub WndProc(ByRef m As System.Windows. Forms.Message)
If m.Msg = WM_KEYUP Then
Return
End If
MyBase.WndProc( m)
End Sub
End Class
#End Region
#Region "DataGridComboB oxColumn"
Public Class DataGridComboBo xColumn
Inherits DataGridTextBox Column
Public myCombo As DataGridComboBo x
Private m_isEditing As Boolean
Public Sub New()
MyBase.New()
myCombo = New DataGridComboBo x
AddHandler myCombo.Leave, New EventHandler(Ad dressOf LeaveComboBox)
AddHandler myCombo.Selecti onChangeCommitt ed, New
EventHandler(Ad dressOf OnSelectionChan geCommitted)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager ,
ByVal Rownum As Integer, _
ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, ByVal
instantText As String, ByVal _
cellIsVisible As Boolean)
MyBase.Edit(sou rce, Rownum, bounds, readOnly1, instantText,
cellIsVisible)
myCombo.Parent = Me.TextBox.Pare nt
myCombo.Locatio n = Me.TextBox.Loca tion
myCombo.Size = New Size(Me.TextBox .Size.Width, myCombo.Size.He ight)
myCombo.Text = Me.TextBox.Text
Me.TextBox.Visi ble = False
myCombo.Visible = True
myCombo.BringTo Front()
myCombo.Focus()
End Sub
Private Sub LeaveComboBox(B yVal sender As Object, ByVal e As EventArgs)
myCombo.Hide()
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As
CurrencyManager , ByVal rowNum As Integer) As Boolean
If m_isEditing Then
m_isEditing = False
SetColumnValueA tRow(dataSource , rowNum, myCombo.Text)
End If
Return True
End Function
Private Sub OnSelectionChan geCommitted(ByV al sender As Object, ByVal e As
EventArgs)
m_isEditing = True
MyBase.ColumnSt artedEditing(CT ype(sender, Windows.Forms.C ontrol))
End Sub
End Class
#End Region
Then, here's an example of adding it to the the TableStyle:
Dim csExcelJob As New DataGridTextBox Column
Debug.Assert(Al lData.Tbl_Job_T racking.Columns (11).ColumnName =
"Excel_Job_numb er")
With csExcelJob
.MappingName = AllData.Tbl_Job _Tracking.Colum ns(11).ColumnNa me
.HeaderText = "Facilty Job#"
.Width = 85
.NullText = ng
End With
tsMain.GridColu mnStyles.Add(cs ExcelJob)
'Delete Code Column
Dim csDeleteCode As New DataGridComboBo xColumn
With csDeleteCode
.MappingName = AllData.Tbl_Job _Tracking.Colum ns(12).ColumnNa me
.HeaderText = "Delete Code"
.Width = 85
.NullText = ng
End With
With csDeleteCode.my Combo.Items
.Add("DS")
.Add("ND")
.Add("BA")
.Add("CB")
.Add("CW")
.Add("ID")
.Add("MD")
End With
csDeleteCode.my Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
tsMain.GridColu mnStyles.Add(cs DeleteCode)
dg.TableStyles. Add(tsMain)
"Kevin" <an*******@disc ussions.microso ft.com> wrote in message
news:92******** *************** ***********@mic rosoft.com...
Hi All
I want to add two combobox columns in my datagrid. the one combobox column
must be bound to the same datasource that the datagrid is, and the other
combobox I just want to populate with a whole lot of values, and then when
the user selects a value, I want it to filter the possible values in the
other combobox. Is this possible? Also this unbound combobox that hold the
whole lot of values, I want to insert it next to the combobox that will be
filtered.
I have a typed dataset with all the relevant tables, I have a Data access
layer that exposes this typed dataset and has methods that populats the
datatables in the typed dataset.
My Datagrid is bound to a datatable called Jobcards. Now I want a combobox
column(the unbound one) to have a list of all the possible customers, and
when the user selects the customer I want the second combobox(which is
bound) to be filtered to only the possible contact people for that specific
customer and it must store that specific customer contact ID. I have got two
tables in a database. One is called Customers, and the other
CustomerContact s and the two have a relationship based on the customerID.
What I want to know after this whole long explanation is, Can I do this,
and if so how? Datagrids are quite new to me, so I am anticipating alot of
reading! Please can somebody point me in the right direction, and/or provide
me with and explantion of how to do this. I have managed to insert 1
combobox column and bind it to a datasource but I'm having difficulty adding
that column inbetween other columns! So I suppose this is a start, but I
don't know how to add the unbound column to my datagrid and do all that
filtering stuff?
Please help... I aplogise if my english is bad...
TIA
Kevin