Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Sorting Relational data

Back to Forum: Tech Off
  • User profile image
    qwert231

    I've got a listbox that's bound like this:

    Me.lbItems.DisplayMember = "Format.FormatLit.ItemName"

    Me.lbItems.ValueMember = "Format.FormatLit.ItemID"

    Me.lbItems.DataSource = Me.dsLit

    I want the items sorted by ItemName. This is pulling from and XML file. Any way to do this?

    (Setting Sorted=True on the listbox gives me System.Item or something in the listbox instead of my items.)

  • User profile image
    Ang3lFir3

    ME.dsLit.Tables.Item(0).DefaultView.Sort = "ItemName ASC"
    ME.lbItems.DataSource = Me.dsLit.Tables.Item(0).DefaultView


    for more control and reusability of your dataset bind the listbox to a seperate dataview instead(not the defaultview)... though personally i would just adjust the defaultview.sort property as needed in code but that is dependant on your implementation

    Dim dvLit as New DataView(dsLit.Tables.Item(0))
    dvLit.Sort = "ItemName ASC"

    ME.lbItems.DataSource = dvLit

    give em a shot....

    reference: http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort(VS.80).aspx"> http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort(VS.80).aspx

  • User profile image
    amigus

    You didn't mention if you're using 2.0 or not but if you are try using System.Windows.Forms.BindingSource().  If not then AFAIK you must use the DataView in which case I second creating a new view from the table rather than using .DefaultView.

  • User profile image
    qwert231

    Hmm... will that work with a relation? See dsLit is the dataset, and if you look at the listbox binding, I'm binding the Value and Display members to parts of the relation. This listbox is my second listbox, the first being format. Selecting a different format selects a different set of literature, due to the binding.

    And it's .Net 1.1

    Example:
    Table: Format
    FormatID   Format
    1               Book
    2               Magazine
    3               CD
    Bound to lbFormat:
    lbFormat.DisplayMember = "Format.Format"
    lbFormat.ValueMember = "Format.FormatID"
    lbFormat.DataSource = dsLit ' A dataSET


    Table: Literature
    ItemID  FormatID  ItemName
    1           1              Some Book
    2           2              Linux Today
    3           1              Some Otha Book
    Bound to lbLiterature:
    lbLit.DisplayMember = "Format.FormatLit.ItemName" ' FormatLit being the name of the relation between Format and Literature
    lbLit.ValueMember = "Format.FormatLit.ItemID"
    lbLit.DataSource = dsLit

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.