how to display two columns in a listBox and when is clikc have the value of the first column

I have a listbox that needs to display two columns like this:

***      Doe
111    Smith
222    Last

i'm getting these record from a query string:
Select REP_P, Name FROM tblBrokers order by REP_P

so when the forms loads i need to display these records in columns, now i have the MultipleColumns options set to true but how do i enable to display two columns?

Who is Participating?
Praveen KumarArchitectCommented:
nice to hear that...

rag the ListView into the form, and create the( two in your case) columns, from Columns property of ListView.

once you got the data into a datatable or dataset, you show in the listview like this...

  For Each row As DataRow In dtContracts.Rows
              ListViewItem item = new ListViewItem();
   End For      


Do you want to display the values in 2 columns ?

Or do you want the listbox to show 2 columns (something like a grid, with in the first column rep_p and in the second column name ?)
Praveen KumarArchitectCommented:
if you want to disply twio columns, better to go with ListView control, which have more properties and options.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

jsctechyAuthor Commented:
>>Or do you want the listbox to show 2 columns (something like a grid, with in the first column rep_p and in the second column name ?)

yes, this is what i want.

But the sample you have provide me I dont see how to apply to my problem.

how do i turn my query result into an array or the same way the sample provided does?
I would also take a look at the listview instead of the listbox...
Or a datagrid ...
jsctechyAuthor Commented:
how can i do it with a listview?
jsctechyAuthor Commented:
is that C++?
i'm working on VB .NET
jsctechyAuthor Commented:
if the code you have provide is a C# here is the equivalent in .NET  and when i execute the code below it does not display anything.

For Each row As DataRow In dt.Rows
                Dim item As New ListViewItem()
                item.Text = Convert.ToString(row(0))
                ' End For

You have provide me some good link but the links just show how to hard code values but i need to add values from the result set of the query string.

so to summary my progress i'm still where i started =/
Praveen KumarArchitectCommented:
sorry for that,

change the View property to Details for the ListView
jsctechyAuthor Commented:
ok i see what i was doing wrong:
here is what i have done:
>>create the( two in your case) columns, from Columns property of ListView.
I did not create this so the listView did not know where to put my values ;-)

i have a question though if i'm populating my listview after i click on a buttom how do i clear the context inside the listview?
if i do listview.clear() this will not allow the listview to display any values becasue i will kind of erase the columns and their values!
how can i just emptry the values been display and show the new values?
jsctechyAuthor Commented:
hahahahaha i feel dumb

just add before the click event requeries the items this line:

listView.items.Clear ()

thanks for all your help
Praveen KumarArchitectCommented:
glad to help you.
thanks for your points, which gave me a premiem membership.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.