|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Error "Cast from type 'DataRowView' to type 'String' is not valid"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 Hi,
Show quote "Richard Mueller" <RichardMuel***@discussions.microsoft.com> wrote in You're probely getting this error because SelectedValueChanged fires after message news:0DDE9F39-F174-44E2-8F8C-6176F4ED484D@microsoft.com... > 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? setting the ComboBox.DataSource but before you have set the ComboBox.ValueMember. So at that point SelectedValue returns a DataRowView. So you could change it to: Private Sub ComboBox1_SelectedValueChanged(....) Dim strFilter As String If (ComboBox1.SelectedIndex <> -1) AndAlso _ (ComboBox1.ValueMember<>"") Then strFilter = "ID = " & ComboBox1.SelectedValue MyDataView.RowFilter = strFilter End If End Sub BUT, if you have a DataRelation between the two DataTables, then you don't need to do that, you can let the child rows be _automatically_ filtered. All you have to do is bind the DataGrid to the same DataSource as the ComboBox and set DataGrid.DataMember to the name of the DataRelation between them: 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 DataGrid1.DataSource = MyDataSet.Tables(0) DataGrid1.DataMember = "NameOfRelationBetweenTables" End Sub This way the DataGrid should be auto filtered when you change ComboBox selection. HTH, Greetings Show quote > 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 Bart,
Thank you very much. Using the DataRelation worked. I just had to specify the "RelationName" attribute of my DataRelation: DataGrid1.DataSource = MyDataSet.Tables(0) DataGrid1.DataMember = MyDataRelation.RelationName Richard Show quote "Bart Mermuys" wrote: > Hi, > > "Richard Mueller" <RichardMuel***@discussions.microsoft.com> wrote in > message news:0DDE9F39-F174-44E2-8F8C-6176F4ED484D@microsoft.com... > > 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? > > You're probely getting this error because SelectedValueChanged fires after > setting the ComboBox.DataSource but before you have set the > ComboBox.ValueMember. So at that point SelectedValue returns a DataRowView. > So you could change it to: > > Private Sub ComboBox1_SelectedValueChanged(....) > Dim strFilter As String > If (ComboBox1.SelectedIndex <> -1) AndAlso _ > (ComboBox1.ValueMember<>"") Then > strFilter = "ID = " & ComboBox1.SelectedValue > MyDataView.RowFilter = strFilter > End If > End Sub > > BUT, if you have a DataRelation between the two DataTables, then you don't > need to do that, you can let the child rows be _automatically_ filtered. > All you have to do is bind the DataGrid to the same DataSource as the > ComboBox and set DataGrid.DataMember to the name of the DataRelation between > them: > > 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 > > DataGrid1.DataSource = MyDataSet.Tables(0) > DataGrid1.DataMember = "NameOfRelationBetweenTables" > End Sub > > This way the DataGrid should be auto filtered when you change ComboBox > selection. > > > HTH, > Greetings > > > > > 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 > > > > > > |
|||||||||||||||||||||||