• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

LISTBOX ... names in List ie: John Doe, Anna Smith ... Order by last name How?

How do I do this so that when names are loaded into Listbox they are in order by last name

I am getting
Anna Smith
John Doe
I need
John Doe
Anna Smith
0
vbMarkO
Asked:
vbMarkO
  • 5
  • 4
1 Solution
 
CodeCruiserCommented:
How are the names stored? How are you retrieving the names? Are the names split into firstname and lastname? If yes, you can do

Select FirstName + ' ' + LastName As FullName From mytable Order By LastName
0
 
vbMarkOAuthor Commented:
The names are in an XML and are called by this code

 Dim doc = XDocument.Load(myPath)

        Dim Cons = From xe In doc...<Contact>
                   Select xe

        For Each itms In Cons
            lstMem.Items.Add(itms.@Fname & " " & itms.@Lname)
        Next
0
 
CodeCruiserCommented:
Try changing to following

Dim dTable As New DataTable
dTable.Columns.Add("FName", GetType(String))
dTable.Columns.Add("LName", GetType(String))

For Each itms In Cons
   Dim drow As DataRow = dTable.NewRow()
   drow(0) = itms.@Fname
   drow(1) = itms.@:name
   dTable.Rows.Add(drow)
Next
dTable.Columns.Add("FullName", GetType(String), "FName + ' ' + LName")
dTable.DefaultView.Sort = "LName"
lstMem.DisplayMember = "FullName"
lstMem.ValueMember = "FullName"
lstMem.DataSource = dTable.DefaultView
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
vbMarkOAuthor Commented:
Ok changed it to this please let me know if I messed something

Dim doc = XDocument.Load(myPath)

        Dim Cons = From xe In doc...<Contact>
                   Select xe
        Dim dTable As New DataTable
        dTable.Columns.Add("FName", GetType(String))
        dTable.Columns.Add("LName", GetType(String))
        For Each itms In Cons
            'lstMem.Items.Add(itms.@Fname & " " & itms.@Lname)
            Dim drow As DataRow = dTable.NewRow()
            drow(0) = itms.@Fname
            drow(1) = itms.@Lname
            dTable.Rows.Add(drow)
        Next
        dTable.Columns.Add("FullName", GetType(String), "FName + ' ' + LName")
        dTable.DefaultView.Sort = "LName"
        lstMem.DisplayMember = "FullName"
        lstMem.ValueMember = "FullName"
        lstMem.DataSource = dTable.DefaultView

I am now getting an error here ... note didnt have before

Private Sub lstMem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMem.SelectedIndexChanged
        Dim doc = XDocument.Load(myPath)

        Dim Cons = From xe In doc...<Contact> _
                  Where xe.@Fname & " " & xe.@Lname = lstMem.SelectedItem _
                  Select xe

        txtfName.Text = Cons.@Fname
        txtLname.Text = Cons.@Lname
        txtAdd.Text = Cons.<Address>.Value
        txtCity.Text = Cons.<City>.Value
        txtState.Text = Cons.<State>.Value
        txtHomeNum.Text = Cons.<HomePhone>.Value
        txtCellNum.Text = Cons.<CellPhone>.Value
        rtb1.Text = Cons.<Notes>.Value





    End Sub

Error sais

Overload resolution failed because no Public '=' can be called with these arguments:
    'Public Shared Operator =(a As String, b As String) As Boolean':
        Argument matching parameter 'b' cannot convert from 'DataRowView' to 'String'.
0
 
CodeCruiserCommented:
Try changing

 lstMem.SelectedItem

to

 lstMem.SelectedText

Or

CType( lstMem.SelectedItem, DataRowView)("FullName")
0
 
vbMarkOAuthor Commented:
No SelectedText I could find anyway.

Ctype    Worked

Though I am not familiar with data tables or sets what ever they are called it does work ...

can count this as a solution ... but might I ask ... for a non data thing guy like me ... couldnt I use a list of string or array of some sorts to sort them and add it to the listbox ...

IF needed I can go ask this in another question if that is a complicated question and or big can of worms
0
 
CodeCruiserCommented:
0
 
CodeCruiserCommented:
But the trouble is you want to display fullname but sort by surname so I think DataTable is the best choice in that situation.
0
 
vbMarkOAuthor Commented:
This got it done ... and the Expert even offered other suggestions

I appreciate it very much.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now