2 combo boxes in datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi Al

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 CustomerContacts 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..

TI
Kevin
 
sorry but I forgot to mention that this is a windows datagrid not an ASP one, and I'm using win XP with VS 200
thank
Kevin
 
Here's some code on MSDN (I Didn't write it) for creating a
DataGridComboBoxColumn and DataGridComboBox. Since the DataGridComboBox
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 "DataGridComboBox"
Public Class DataGridComboBox
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 "DataGridComboBoxColumn"
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public myCombo As DataGridComboBox
Private m_isEditing As Boolean
Public Sub New()
MyBase.New()
myCombo = New DataGridComboBox
AddHandler myCombo.Leave, New EventHandler(AddressOf LeaveComboBox)
AddHandler myCombo.SelectionChangeCommitted, New
EventHandler(AddressOf OnSelectionChangeCommitted)
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(source, Rownum, bounds, readOnly1, instantText,
cellIsVisible)
myCombo.Parent = Me.TextBox.Parent
myCombo.Location = Me.TextBox.Location
myCombo.Size = New Size(Me.TextBox.Size.Width, myCombo.Size.Height)
myCombo.Text = Me.TextBox.Text
Me.TextBox.Visible = False
myCombo.Visible = True
myCombo.BringToFront()
myCombo.Focus()
End Sub
Private Sub LeaveComboBox(ByVal 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
SetColumnValueAtRow(dataSource, rowNum, myCombo.Text)
End If
Return True
End Function
Private Sub OnSelectionChangeCommitted(ByVal sender As Object, ByVal e As
EventArgs)
m_isEditing = True
MyBase.ColumnStartedEditing(CType(sender, Windows.Forms.Control))
End Sub
End Class
#End Region

Then, here's an example of adding it to the the TableStyle:

Dim csExcelJob As New DataGridTextBoxColumn
Debug.Assert(AllData.Tbl_Job_Tracking.Columns(11).ColumnName =
"Excel_Job_number")
With csExcelJob
.MappingName = AllData.Tbl_Job_Tracking.Columns(11).ColumnName
.HeaderText = "Facilty Job#"
.Width = 85
.NullText = ng
End With
tsMain.GridColumnStyles.Add(csExcelJob)
'Delete Code Column
Dim csDeleteCode As New DataGridComboBoxColumn


With csDeleteCode
.MappingName = AllData.Tbl_Job_Tracking.Columns(12).ColumnName
.HeaderText = "Delete Code"
.Width = 85
.NullText = ng
End With
With csDeleteCode.myCombo.Items
.Add("DS")
.Add("ND")
.Add("BA")
.Add("CB")
.Add("CW")
.Add("ID")
.Add("MD")
End With
csDeleteCode.myCombo.DropDownStyle = ComboBoxStyle.DropDownList

tsMain.GridColumnStyles.Add(csDeleteCode)
dg.TableStyles.Add(tsMain)
Kevin said:
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
CustomerContacts 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?
 
Hi Willia

Thanks for the reply, I guess I'll have to try to convert this to C#. Anyway, I was thinking, how do I keep the two comboboxes in sync with each other, because obvoiously each row is going to have a different combination of CustID vs custDetailsID. So how do I keep these two comboboxes in sync while still keeping the CustDetaisID in sync with the typed dataset's datatable.

TI
Kevin
 
Back
Top