Multiple columns in list box

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am using the following code to populate my list box. How can I now have
multiple columns in the list box? At the moment there is only one column
company that's displayed.

Thanks

Regards


Private dv As DataView

dv = New DataView(ds.tblClients)

lstLocate.DataSource = dv

lstLocate.DisplayMember = "Company"
 
-----Original Message-----
Hi

I am using the following code to populate my list box. How can I now have
multiple columns in the list box? At the moment there is only one column
company that's displayed.

Thanks

Regards


Private dv As DataView

dv = New DataView(ds.tblClients)

lstLocate.DataSource = dv

lstLocate.DisplayMember = "Company"


.
John,
Short of creating a custom listbox, to my knowledge,
there's no way to display multiple columns. If you are
not planning on modifying the data, It sounds to me that
your situation calls for a datareader populating a
listview. The listview in details view makes a very nice
display. Set the FullRowSelect and grid properties to
true. Add column headers and then use the
SqlDataReader.Read() method to read the data, each time
adding an item to the listview. Here's some sample code:

while(dr.Read())
{
//create the list view item
ListViewItem itmX = new ListViewItem();

//Specify the text and subitems of listview
itmX.Text = dr.GetValue(0).ToString();

for(int i = 1; i < dr.FieldCount; i++)
{
itmX.SubItems.Add(dr.GetValue(i).ToString());
}
listViewResults.Items.Add(itmX);
}

Make sure you enclose it in a try-catch block as always
when working with a remote call or file access. And be
sure to close the dataReader and the connection in the
finally block of the try. Good luck.
 
Hi

I am not totally sure but I have come up with this code (below). It gives
error on the specified line.

dv = New DataView(ds.tblClients) ' I am getting data from a dataview
Dim itmX As New ListViewItem
Dim i As Long
For i = 1 To dv.Count
itmX.Text = "Item Text " & i.ToString
itmX.SubItems.Add(dv.Item(i).Item("ID")) ' <=== Errror 'Index 1 is not
non-negative and below total rows count.' on this line.
itmX.SubItems.Add(dv.Item(i).Item("Company"))
itmX.SubItems.Add(dv.Item(i).Item("Address_1"))
lstLocate.Items.Add(itmX) ' lstLocate is my ListView control on
the form
Next

Any help would be appreciated.

Thanks

Regards
 
Back
Top