[02/03] A few basic questions on DataView and DataBinding (Mainly syntax)
1. If I create a sorted DataView object from a DataSet table, how do I
reference the sorted elements? (Say I want to output the CustomerName
for the first row (in the sorted DataView) to a label or textbox)
VB Code:
Dim view As DataView = New DataView(dsDataset.Customer)
view.Sort = "CustomerID ASC"
2. If after displaying the CustomerName above for the first row in the
sorted DataView, how do I then change it's value. Normally with a
DataSet I'd use:
VB Code:
dsDataset.Customer.Rows(intRowPos)("CustomerName") = "John Smith"
But how would it be done using the DataView. I know a DataView is read
only so obviously I'll have to pass a paramater to the Dataset code
above so it knows, that "this DataView row matches this DataSet table
row".
3.
VB Code:
lblCustomerID.DataBindings.Add("Text", dsDataset,
"Customer.CustomerID")
lblCustomerAddress.DataBindings.Add("Text", dsDataset,
"Customer.CustomerAddress")
cmbCustomerName.DataSource = dsDataset
cmbCustomerName.DisplayMember = "Customer.CustomerSurname"
cmbCustomerName.ValueMember = "Customer.CustomerID"
The above code populates and binds a ComboBox so that when a Surname is
selected it simply outputs the related CustomerID and CustomerAddress
to labels. How do I make it so that both CustomerSurname and the
related CustomerForename both appear in the ComboBox?
4. What's the alternative to Q3 if I don't wanna use Binding. This is
what i have so far:
VB Code:
Private Sub FillComboBox()
Dim intCustomerRow As Integer = 0
cmbCustomerName.Items.Clear()
Do Until intCustomerRow = dsDataset.Customer.Rows.Count
cmbCustomerName.Items.Add(dsDataset.Customer.Rows(intRowPos)_
("CustomerSurname").ToString() & ", " &
dsDataset.Customer.Rows(intRowPos)_
("CustomerForename").ToString())
intCustomerRow = intCustomerRow + 1
intRowPos = intRowPos + 1
Loop
End Sub
But all this does is fill the ComboBox with the Forenames and Surnames.
I've tried loads of code in cmbCustomerName_SelectedIndexChanged but
none works.
I know these are basic Q's, but they also tend to be difficult to find
since everyone assumes everyone else knows it.
Re: [02/03] A few basic questions on DataView and DataBinding (Mainly syntax)
DataViews are NOT read-only and you actually don't even have to create one under most circumstances. Every DataTable already has a DataView associated with it through its DefaultView property. Instead of this:
VB Code:
Dim view As DataView = New DataView(dsDataset.Customer)
view.Sort = "CustomerID ASC"
you can do this:
VB Code:
dsDataset.Customer.DefaultView.Sort = "CustomerID ASC"
You can then access the rows in the DataView in a very similar fashion to the rows in the DataTable. Instead of this:
VB Code:
dsDataset.Customer.Rows(intRowPos)("CustomerName") = "John Smith"
you can do this:
VB Code:
dsDataset.Customer.DefaultView(intRowPos)("CustomerName") = "John Smith"
Note that the Rows property of a DataTable is a DataRowCollection object. The DataView is very similar to that in a lot of ways. A DataRowCollection contains DataRows while a DataView contains DataRowViews, but each DataRowView can return its fields in exactly the same way as a DataRow can: either by numerical index or column name. Note that you can also get the DataRow that corresponds to a DataRowView from its Row property. Try this for an exercise:
VB Code:
dsDataset.Customer.DefaultView.Sort = "CustomerID ASC"
'Will display surnames from the table in original order.
For Each row As DataRow In dsDataSet.Customer.Rows
MessageBox.Show(row("CustomerSurname"))
Next row
'Will display surnames from the view in sorted order.
For Each view As DataRowView In dsDataSet.Customer.DefaultView
MessageBox.Show(view("CustomerSurname"))
Next view
'Will display surnames from the table in sorted order.
For Each view As DataRowView In dsDataSet.Customer.Default
MessageBox.Show(view.Row("CustomerSurname"))
Next view