How do I use a combobox for master-detail binding with a datagrid?

L

Larry Woods

I have a combobox that is populated with a list of names. When a name is
selected I want to populate a datagrid with name information. I have my
master-detail defined in my dataset and it works fine if I use a datagrid
for the master, instead of a combobox. But the combobox won't cause the
repopulation of the datagrid with the appropriate data. What's the trick?

TIA,

Larry Woods
cross-posted on ...dotnet.languages.vb.controls
 
K

Ken Tucker [MVP]

Hi,

Create a dataview to for the datagrid. Bind the datagrid to the
dataview. Filter the records in the combobox selected item changed event.

Dim ds As New DataSet

Dim dv As DataView

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strConn As String

Dim strSQL As String

Dim da, daOrders As OleDbDataAdapter

Dim conn As OleDbConnection

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"

conn = New OleDbConnection(strConn)

da = New OleDbDataAdapter("Select * From Customers", conn)

da.Fill(ds, "Customers")

daOrders = New OleDbDataAdapter("Select * From Orders", conn)

daOrders.Fill(ds, "Orders")

dv = New DataView(ds.Tables("Orders"))

ComboBox1.DataSource = ds.Tables("Customers")

ComboBox1.DisplayMember = "CustomerID"

DataGrid1.DataSource = dv

End Sub

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles ComboBox1.SelectedValueChanged

Dim drv As DataRowView

Try

drv = DirectCast(ComboBox1.SelectedItem, DataRowView)

dv.RowFilter = "CustomerID = '" & drv.Item("CustomerID").ToString & "'"

Catch ex As Exception

End Try

End Sub





Ken
----------------------
I have a combobox that is populated with a list of names. When a name is
selected I want to populate a datagrid with name information. I have my
master-detail defined in my dataset and it works fine if I use a datagrid
for the master, instead of a combobox. But the combobox won't cause the
repopulation of the datagrid with the appropriate data. What's the trick?

TIA,

Larry Woods
cross-posted on ...dotnet.languages.vb.controls
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top