Populate a listview from a datatable

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hello All,

I was wondering if someone might be able to point me in the right direction when attempting to populate a listview control by means of a datatable? Could anyone provide a working example of this kind of iteration?

Cheers,

sfx
 
I am trying to do the same thing I think. This is what I am trying to do...
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] query [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Select name From identification"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dsTest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] daTest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(query, cnxn)
daTest.Fill(dsTest, "Test")
 
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2]i = 0
[/SIZE][SIZE=2][COLOR=#0000ff]for[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]each[/COLOR][/SIZE][SIZE=2] dstest.Tables("Test").Rows(i) [/SIZE][SIZE=2][COLOR=#008000]'<-----Have a "syntax error" here
[/COLOR][/SIZE][SIZE=2]lstdocs.Items.Add( [/SIZE][SIZE=2][COLOR=#008000]'<------dont know what to do here
[/COLOR][/SIZE][SIZE=2]i = i + 1
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/SIZE]

I know that I am missing something in my loop

I also tried this...
VB.NET:
[SIZE=2]lstDocs.DataSource = dsTest.Tables("Test")
[/SIZE]

But all I got in my List box was "System.Data.DataRowView" written over and over

Any help is appreciated. Thanks
 
or maybe something like this???
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataRow[/SIZE]
[SIZE=2][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] dsTest.Tables("Test").Rows
lstDocs.Items.Add(dr("name"))
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][/SIZE]
 
Hi Evad,

After a bit of trial and error I have found an easy way to populate a ListView control from a DataTable. It involves the instantiation of a new ListViewItem object for each row in the DataTable and then subsequently adding each instantiation to the ListView control. The below code needs to be added to a sub procedure.

' ListView1 is a ListView control added at design time *with* columns.
' objDataTable is a previously created DataTable that has been filled by a DataAdapter.

Dim lvwItem As ListViewItem

For i As Integer = 0 To objDataTable.Rows.Count - 1

lvwItem =
New ListViewItem
lvwItem.Text = objDataTable.Rows(i).Item(
"Column1").ToString
lvwItem.SubItems.Add(objDataTable.Rows(i).Item(
"Column2").ToString)

ListView1.Items.Add(lvwItem)

Next i

I hope this may be of help.

Cheers,

sfx1
 
There's an even easier way. Use the ExtendedListView control in the WFC library in my signature. It supports complex data-binding like the DataGrid, so you just have to set a few properties including the DataSource. The library is compiled for .NET 1.1 but I've tested it in a .NET 2.0 app and the ExtendedListView seems to work fine. I can also vouch for the NumberBox but unfortunately the ExtendedDateTimePicker falls over.
 
Hey Guys, Thanks for the input.

I like what you added sfx, my next question was going to be how do I get two columns from my data set into the same list box. Now working with what you have given I have new questions.

First, what is the difference between a listview and a listbox?

I tried using what you had in your last reply sfx for my list with confusing reults. This is what I have:
VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lvwItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] dsTest.Tables("test").Rows.Count - 1
lvwItem = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem
lvwItem.Text = dsTest.Tables("test").Rows(i).Item("name").ToString
lvwItem.SubItems.Add(dsTest.Tables("test").Rows(i).Item("title").ToString)
ListView1.Items.Add(lvwItem)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i[/SIZE]

Pretty much the same as what you had. When I do this my list view looks very odd. Instead of listing all the items top to bottom, my list view is listing items left to right. Is this how a list view behaves?

So I tried the same code above but put it into a list box. I made only one change. Instead of ListView1.Items.Add(lvwItem), I have lstBox1.Items.Add(lvwItem). With this my list box populates like the following:
ListViewItem: {Dave, Mr.}
ListViewItem: {Sue, Miss.} and so on

I am pretty new to this .Net stuff so I am sure that I am missing something simple.

Thanks for all the help guys
 
A ListBox is designed to display a simple list, i.e. one column of data. You can use them to display a single column of a DataTable but trying to use them to display tabular data is a bad idea. A ListView can display your data in a number of ways. The right hand side of Windows Explorer is a ListView. If you're using .NET 1.1 then you may be better off using a DataGrid. If you're using .NET 2.0 then you're almost certainly better off using a DataGridView. Please always specify what version you're using because the solution is often different for different versions. It's as simple as setting your primary platform in your profile so we can see it on every post. Help us to help you.
 
Thanks for the explination. Sorry about the platform thing. I am using .Net 1.1 Visual Studio 2003.

After reading your explination, I definately want to use a Listbox. Thanks

I am just confused now as to why after I populate my listbox every item in the list box is preceeded with "ListViewItem {". Am I not supposed to use ListViewItem here? This is what I have
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] query [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Select employeeid, name, title From identification Order By name"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dsTest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataSet = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] DataSet
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] daTest [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlDataAdapter = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlDataAdapter(query, cnxn)
daTest.Fill(dsTest, "Test")
[/SIZE]
 
[SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lvwItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ListViewItem
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] dsTest.Tables("test").Rows.Count - 1
lvwItem = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewItem
lvwItem.Text = dsTest.Tables("test").Rows(i).Item("employeeid").ToString + " " _
+ dsTest.Tables("test").Rows(i).Item("Name").ToString + " " _
+ dsTest.Tables("test").Rows(i).Item("title").ToString
ListBox1.Items.Add(lvwItem)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i
[/SIZE]

Like I said, every item in my listbox looks like this:
ListViewItem {1 Dave Mr.}

What am I doing wrong?
 
Ok, I figured out...took me long enough

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] text [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] dsTest.Tables("test").Rows.Count - 1
text = dsTest.Tables("test").Rows(i).Item("employeeid").ToString + " " _
+ dsTest.Tables("test").Rows(i).Item("name").ToString + " " _
+ dsTest.Tables("test").Rows(i).Item("title").ToString
ListBox1.Items.Add(text)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i
[/SIZE]

Thanks again for all the noob help
 
I don't quite understand why you've chosen to use the ListBox. each of your items contains three pieces of data. Wouldn't it be better to have a ListView or DataGrid with three columns? That way each corresponding piece of data is kept in line vertically and you can sort on whichever column you choose. In my opinion a ListBox would look considerably less professional in this case. If you're ahppy with the result then I guess that's the main thing though.
 
I am not looking to sort this information but rather display it as a series of strings in a list format. My above examples were just me testing out how to do this sort of thing. What I am ultimately doing is listing my software inventory. Then I want a user to be able to select a certain piece of software and then view information on that software. So my list will look something like this:

Microsoft Office v2000
Microsoft Publisher v2000
Vendor Title Version....

Then I have my app set up so a user can select some software from the list and view documentation or notes or even install it. So I really like the listbox for this. But please, if there is a better way of doing something like this I would really appreciate any further assistance that you could offer.

Thanks again for all the assistance this stuff is huge for noobies
 
Hi Evad,

Sorry for not getting back to you sooner. For some reason I had not been receiving email notification of replies to my postings. As an aside, using a "For Each" looping construct is probably even simpler than a "For Next" loop when considering what you want to achieve. The reason for this is that a "For Each" loop is specifically designed for collection iterations.

jmcilhinney,

The custom controls on your website are fantastic. Well done! It's nice to be able to correspond with someone who really knows their stuff. :)

Cheers,

sfx
 
sfx said:
jmcilhinney,

The custom controls on your website are fantastic. Well done! It's nice to be able to correspond with someone who really knows their stuff. :)
I'm sorry to have to say that those controls are nothing to do with me. I've just provided some links to useful resources but I can take no credit for their creation. I'd still like to think that I know my stuff though. ;)
 
Back
Top