G
Guest
I've seen this problem posted several times, but have not yet seen a
solution. I make a connection to a SQL database and use a DataAdapter object
to fill a DataSet object with several tables. A DataRelation object links the
tables by an ID column. I bind the parent table to a combo box and a child
table to a DataGrid control using a DataView object. I want the DataGrid
control to reflect the selection in the combo box. For the combo box,
DisplayMember is a text string while ValueMember is the ID field (integer).
In brief:
Private Sub Form1_Load(....)
' Connect to database, populate Tables in DataSet,
' setup DataViews, and define DataRelations.
Call Populate()
ComboBox1.DataSource = MyDataSet.Tables(0)
ComboBox1.DisplayMember = MyDataSet.Tables(0).Columns(1).ColumnName
ComboBox1.ValueMember = MyDataSet.Tables(0).Columns(0).ColumnName
Me.BindingContext(MyDataSet.Tables(0)).Position = 0
Dim strFilter As String
strFilter = "ID = " & ComboBox1.SelectedValue
MyDataView.RowFilter = strFilter
DataGrid1.DataSource = MyDataView
End Sub
Private Sub ComboBox1_SelectedValueChanged(....)
Dim strFilter As String
If (ComboBox1.SelectedIndex <> -1) Then
strFilter = "ID = " & ComboBox1.SelectedValue
MyDataView.RowFilter = strFilter
End If
End Sub
Strangely, the code works if I comment out the
ComboBox1_SelectedValueChanged sub. I can hard code in Form1_Load to select
the first (index=0), or any other item in the combo box, and the DataGrid
reflects the correct item. But of course I cannot select another item in the
combo box and have the DataGrid reflect this. The error is raised in
CombBox1_SelectedValueChanged on the statement:
strFilter = "ID = " & ComboBox1.SelectedValue
and the error is "Cast from type 'DataRowView' to type 'String' is not
valid. I've tried to use ToString and CStr to no avail. I think the key is
that I assign a DataSource to the combo box, plus the DisplayMember and
ValueMember properites, then attempt to use the SelectedValue property when
the user makes a selection. Any ideas?
Note. My code is based on "Using a Data Set with Tables in a Parent-Child
Relationship" from Chapter 10 (pg. 402-408) of "Programming Microsoft SQL
Server 2000 with Microsoft Visual Basic .NET" by Rick Dobson.
Richard
solution. I make a connection to a SQL database and use a DataAdapter object
to fill a DataSet object with several tables. A DataRelation object links the
tables by an ID column. I bind the parent table to a combo box and a child
table to a DataGrid control using a DataView object. I want the DataGrid
control to reflect the selection in the combo box. For the combo box,
DisplayMember is a text string while ValueMember is the ID field (integer).
In brief:
Private Sub Form1_Load(....)
' Connect to database, populate Tables in DataSet,
' setup DataViews, and define DataRelations.
Call Populate()
ComboBox1.DataSource = MyDataSet.Tables(0)
ComboBox1.DisplayMember = MyDataSet.Tables(0).Columns(1).ColumnName
ComboBox1.ValueMember = MyDataSet.Tables(0).Columns(0).ColumnName
Me.BindingContext(MyDataSet.Tables(0)).Position = 0
Dim strFilter As String
strFilter = "ID = " & ComboBox1.SelectedValue
MyDataView.RowFilter = strFilter
DataGrid1.DataSource = MyDataView
End Sub
Private Sub ComboBox1_SelectedValueChanged(....)
Dim strFilter As String
If (ComboBox1.SelectedIndex <> -1) Then
strFilter = "ID = " & ComboBox1.SelectedValue
MyDataView.RowFilter = strFilter
End If
End Sub
Strangely, the code works if I comment out the
ComboBox1_SelectedValueChanged sub. I can hard code in Form1_Load to select
the first (index=0), or any other item in the combo box, and the DataGrid
reflects the correct item. But of course I cannot select another item in the
combo box and have the DataGrid reflect this. The error is raised in
CombBox1_SelectedValueChanged on the statement:
strFilter = "ID = " & ComboBox1.SelectedValue
and the error is "Cast from type 'DataRowView' to type 'String' is not
valid. I've tried to use ToString and CStr to no avail. I think the key is
that I assign a DataSource to the combo box, plus the DisplayMember and
ValueMember properites, then attempt to use the SelectedValue property when
the user makes a selection. Any ideas?
Note. My code is based on "Using a Data Set with Tables in a Parent-Child
Relationship" from Chapter 10 (pg. 402-408) of "Programming Microsoft SQL
Server 2000 with Microsoft Visual Basic .NET" by Rick Dobson.
Richard