|
-
Jan 14th, 2009, 09:08 AM
#1
Thread Starter
PowerPoster
[RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
Hi Guys,
I'm trying the following code:
Code:
Dim dr As DataRow = CType(Me.ProductBindingSource.Current, DataRowView).Row
but I get the error saying:
Code:
Unable to cast object of type 'System.Data.DataViewManagerListItemTypeDescriptor' to type 'System.Data.DataRowView'.
Please help
-
Jan 14th, 2009, 09:12 AM
#2
Re: [2008] Cast bindingsource.current to datarow throwing exception
The items in a BindingSource will be DataRowViews if you have bound a DataTable or a DataView. That looks to me like you've bound a DataSet. Did you do so in the designer or in code? Did you set the DataMember?
-
Jan 14th, 2009, 09:20 AM
#3
Thread Starter
PowerPoster
Re: [2008] Cast bindingsource.current to datarow throwing exception
Hi JM,
I have bound to a dataset like so:
Code:
Dim data As DataSet = Me.GetDataSet
Me.ProductBindingSource.DataSource = data
Me.ProductBindingSource.DataMember = "Parent"
Me.TestBindingSource.DataSource = Me.ProductBindingSource
Me.TestBindingSource.DataMember = "ParentChild"
Me.dgvProducts.DataSource = Me.ProductBindingSource
Me.dgvTests.DataSource = Me.TestBindingSource
the error only comes up when the form loads. however, if I click on the items in my datagridview I am able to get the data I need like this:
Code:
Private Sub ProductBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProductBindingSource.PositionChanged
If (ProductBindingSource.Position > 0) Then
Dim dr As DataRow = CType(Me.ProductBindingSource.Current, DataRowView).Row
MsgBox(dr.Item(0).ToString)
End If
End Sub
Please advise on the best practice for what I am doing.
-
Jan 14th, 2009, 09:21 AM
#4
Re: [2008] Cast bindingsource.current to datarow throwing exception
OK, I had a feeling I knew what you'd done and I just tested the theory and I was right. What you have done is assign a DataSet to the DataSource of the BindingSource and then set the DataMember and DataSource of your control(s). What you should have done is set the DataSource AND DataMember of your BindingSource, then set ONLY the DataSource of your control(s). That way your BindingSource is bound to a DataTable and the items will therefore be DataRowView objects. If the BindingSource is bound to a DataSet then the items are NOT DataRowView objects so the Current property cannot return a DataRowView object, as you can see.
-
Jan 14th, 2009, 09:23 AM
#5
Re: [2008] Cast bindingsource.current to datarow throwing exception
Hmmm... you posted while I was typing and it appears that, while what I said is correct, it doesn't actually apply to you in this case. I'll have to investigate further but not now I'm afraid. It's 1:30 AM here and I need a little sleep before work tomorrow.
-
Jan 14th, 2009, 09:26 AM
#6
Re: [2008] Cast bindingsource.current to datarow throwing exception
One more thing. Are you sure that those DataMember values are actually the names of your DataTables?
-
Jan 14th, 2009, 09:29 AM
#7
Thread Starter
PowerPoster
Re: [2008] Cast bindingsource.current to datarow throwing exception
cool, no problem. I wiil wait for your reply in the morning. Yes i'm sure they are the right names .
-
Jan 14th, 2009, 09:40 AM
#8
Re: [2008] Cast bindingsource.current to datarow throwing exception
I just followed the instructions from my Master/Detail thread to the letter. I then added a Button to the form and added this code:
Code:
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
Dim row As DataRowView = DirectCast(Me.BindingSource1.Current, DataRowView)
MessageBox.Show(CStr(row("Name")))
End Sub
I ran the project and clicked the Button and got the correct, expected result. I changed BindingSource1 to BindingSource2 and tried again and got the correct, expected result. If I changed the DataMember to something invalid I get the same error message as you. You must have used a wrong value somewhere but exactly where is not possible to say from the information we have.
Last edited by jmcilhinney; Jan 14th, 2009 at 06:43 PM.
-
Jan 15th, 2009, 12:52 AM
#9
Thread Starter
PowerPoster
Re: [2008] Cast bindingsource.current to datarow throwing exception
Hi JM,
The error only comes up while the form is loading. After it loads, and when I click on a row, I get the correct result. I'm going to try to fix it now
-
Jan 15th, 2009, 12:58 AM
#10
Re: [2008] Cast bindingsource.current to datarow throwing exception
 Originally Posted by Nitesh
Hi JM,
The error only comes up while the form is loading. After it loads, and when I click on a row, I get the correct result. I'm going to try to fix it now 
Aha!!! The problem, then, is that the PositionChanged event is raised WHEN you set the DataSource property, but at that point you haven't yet set the DataMember property. As such, the DataMember is not the name of a valid table at that point. You should ALWAYS set the DataMember, DisplayMember and ValueMember properties BEFORE setting the DataSource property.
-
Jan 15th, 2009, 01:09 AM
#11
Thread Starter
PowerPoster
Re: [2008] Cast bindingsource.current to datarow throwing exception
Wow. Great. Thank you so much once again. The issue is now fixed
-
Jan 15th, 2009, 01:15 AM
#12
Re: [RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
I should have realised that earlier but I didn't note the significance of this:
the error only comes up when the form loads. however, if I click on the items in my datagridview I am able to get the data I need
from post #3. Sorry about that.
-
Jan 15th, 2009, 01:17 AM
#13
Thread Starter
PowerPoster
Re: [RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
Sorry JM. one more thing. Out of interest I want to know how to do this. I cam across the following code snippet in VB for casting a bindingsource.current to a datarow:
Code:
Dim row As NorthwindDataSet.CustomersRow
row = CType(CType(Me.CustomersBindingSource.Current, DataRowView).Row, NorthwindDataSet.CustomersRow)
I tried to change it according to my code but I was unseccessful. I don't need it right now, but I would like to know the answer
-
Jan 15th, 2009, 01:22 AM
#14
Thread Starter
PowerPoster
Re: [RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
I should have realised that earlier but I didn't note the significance of this:
Quote:
the error only comes up when the form loads. however, if I click on the items in my datagridview I am able to get the data I need
from post #3. Sorry about that.
no need to apologise JM. I'm extremely grateful for your help. I learn alot from you . I've just started my first major app in .Net, and thanks to you, I've learnt all the right coding practices etc, and I'm glad to say I'm about 80% done and I am really confident about my app. Thank you again
-
Jan 15th, 2009, 01:28 AM
#15
Re: [RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
No it's not casting the Current property as type DataRow and it couldn't possibly because it's not. It's casting the Current property as type DataRowView, which is the type the object is. It's then getting the Row property of that DataRowView, which is type DataRow, and casting that as type NorthwindDataSet.CustomersRow. The only reason you would, and could, do that is if you have created a typed DataSet for Northwind database and your BindingSource is bound to the CustomersTable of that DataSet. If you are using an untyped DataSet then there's no need to cast the Row property of the DataRowView. It's type DataRow and that's all you need. This:
vb.net Code:
Dim view As DataRowView = DirectCast(myBindingSource.Current, DataRowView) Dim row As DataRow = view.Row
is the same as this:
vb.net Code:
Dim row As DataRow = DirectCast(myBindingSource.Current, DataRowView).Row
That said, there's generally no point doing that because you can do almost anything with the DataRowView that you can with the DataRow, including deleting or editing the record.
If you are using a typed DataSet then it's worth getting the DataRow and casting it to get strongly-typed access to the data of the record. The code you you provided above is the same as this:
vb.net Code:
Dim view As DataRowView = DirectCast(myBindingSource.Current, DataRowView) Dim untypedRow As DataRow = view.Row Dim row As NorthwindDataSet.CustomersRow = DirectCast(untypedRow, NorthwindDataSet.CustomersRow)
If you're not using the Northwind database or you haven't bound the Customers table then you'd need to adjust that code accordingly.
-
Jan 15th, 2009, 01:44 AM
#16
Thread Starter
PowerPoster
Re: [RESOLVED] [2008] Cast bindingsource.current to datarow throwing exception
Oh ok. I understand now. Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|