Problem with BindingSource (Unsolved)
Hi, I'm trying to migrate from vb6 to vb .net but I have this little problem that doesn't let me keep on working. Do you remember that in vb6 with the DAO control you could reference a field by writing:
dim Customer as string
customer = Data1.recordset.Fields("Custromer_Name")
and you could also modify the field's value by writing
data1.recordset.edit
data.recordset.fields("Customer_Name") = "Juan Lopez"
data1.recordset.update
How do I reference a field with BindingSource???
Thanks for your help.
Re: Problem with BindingSource
If your BindingSource is bound to a DataTable then its items are DataRowView objects that correspond to the DataRows in the DataTable. Because a BindingSource can be bound to all sorts of different objects the items are returned as Object references, so it's up to you to cast them as the appropriate type. Let's say that you have data in a grid bound to a BindingSource that is in turn bound to a DataTable. If you want to set the Customer_Name field of the row that is currently selected in the grid you could do this:
VB Code:
Dim currentRow As DataRowView = TryCast(myBindingSource.Current, DataRowView)
If currentRow IsNot Nothing Then
'There is a row currently selected.
currentRow("Customer_Name") = "Juan Lopez"
End If
Re: Problem with BindingSource
ok. So is not as simple as data1.recordset.fields("Customer_Name"), is it?
And supose that I only want to show a messagebox with the field ("Customer_Name")
In vb6, I would do it like this:
msgbox (data1.recordset.fields("Customer_Name"))
but in VB .net???
Thanks for your help.
Re: Problem with BindingSource
Have a look at this:
VB Code:
[B][U]data.recordset.fields("Customer_Name")[/U][/B] = "Juan Lopez"
and now have a look at this:
VB Code:
msgbox ([B][U]data1.recordset.fields("Customer_Name")[/U][/B])
You see the commonality? Now take a look at this:
VB Code:
[B][U]currentRow("Customer_Name")[/U][/B] = "Juan Lopez"
How do you suppose you would display that field?
Re: Problem with BindingSource
Thank you so much for your help but I have another problem
VB Code:
BindingSource1.Current("Customer_Name") = "Juan Perez"
Me.Validate()
BindingSource1.EndEdit()
customerTableAdapter.Update(Me.CuentasDataSet.Customers)
What's wrong? An error message appears: "Update requires that UpdateCommand is valid when it passes to collection DataRowwith modified rows" (or something similar)
What should I do??
Re: Problem with BindingSource (Unsolved)
Your TableAdapter doesn't have a valid UpdateCommand so you need to make sure it has one. I don't use TableAdapters so I don't know all the details of how they work. They are similar to DataAdapters but they are not the same. This is one of the problems with letting the IDE write a lot of your code for you. If something goes wrong you don't know how to fix it. Even if you didn't have an issue I'd suggest reading up on TableAdapters anyway. Here's a good place to start: http://msdn.microsoft.com/library/de...leadapters.asp
Re: Problem with BindingSource
Here it is
BindingSource.Current("Customer_Name") = "Juan Perez"
Me.Validate()
BindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DataSet)