Results 1 to 2 of 2

Thread: [02/03] A few basic questions on DataView and DataBinding (Mainly syntax)

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    13

    [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:
    1. Dim view As DataView = New DataView(dsDataset.Customer)
    2. 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:
    1. 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:
    1. lblCustomerID.DataBindings.Add("Text", dsDataset,
    2. "Customer.CustomerID")
    3. lblCustomerAddress.DataBindings.Add("Text", dsDataset,
    4. "Customer.CustomerAddress")
    5.  
    6. cmbCustomerName.DataSource = dsDataset
    7. cmbCustomerName.DisplayMember = "Customer.CustomerSurname"
    8. 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:
    1. Private Sub FillComboBox()
    2.         Dim intCustomerRow As Integer = 0
    3.         cmbCustomerName.Items.Clear()
    4.  
    5.         Do Until intCustomerRow = dsDataset.Customer.Rows.Count
    6.  
    7. cmbCustomerName.Items.Add(dsDataset.Customer.Rows(intRowPos)_
    8.           ("CustomerSurname").ToString() & ", " &
    9. dsDataset.Customer.Rows(intRowPos)_
    10. ("CustomerForename").ToString())
    11.             intCustomerRow = intCustomerRow + 1
    12.             intRowPos = intRowPos + 1
    13.         Loop
    14.     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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim view As DataView = New DataView(dsDataset.Customer)
    2. view.Sort = "CustomerID ASC"
    you can do this:
    VB Code:
    1. 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:
    1. dsDataset.Customer.Rows(intRowPos)("CustomerName") = "John Smith"
    you can do this:
    VB Code:
    1. 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:
    1. dsDataset.Customer.DefaultView.Sort = "CustomerID ASC"
    2.  
    3. 'Will display surnames from the table in original order.
    4. For Each row As DataRow In dsDataSet.Customer.Rows
    5.     MessageBox.Show(row("CustomerSurname"))
    6. Next row
    7.  
    8. 'Will display surnames from the view in sorted order.
    9. For Each view As DataRowView In dsDataSet.Customer.DefaultView
    10.     MessageBox.Show(view("CustomerSurname"))
    11. Next view
    12.  
    13. 'Will display surnames from the table in sorted order.
    14. For Each view As DataRowView In dsDataSet.Customer.Default
    15.     MessageBox.Show(view.Row("CustomerSurname"))
    16. Next view
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width