Populating listviews

starman

New member
Joined
Nov 19, 2005
Messages
2
Programming Experience
Beginner
I have a form with a combobox and a listview. The combobox allows me to select a particular table in a database.

I have set up a stored procedure to select just the Firstname and Surname fields from the table.

I have also created a for/next loop to populate the listview with the first x number of rows of the table according to the limits of the loop.

If I set up the loop to populate with just one field then everything works fine. But if I then include the second field, I only get two or three of the field filled. So first column is complete with all names, but the second column contains some blank fields.

When I use a datagrid instead of a list view, the problem does not happen. Any idea why the listview does not populate fully? Code I'm using shown below..

Dim com As SqlCommand = SqlConnection1.CreateCommand
Dim da As SqlDataAdapter = New SqlDataAdapter
com.CommandType = CommandType.StoredProcedure
com.CommandText = "Spring04Confirmed"
da.SelectCommand = com
da.Fill(ds, "Spring04")
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Spring04"
Dim i As Integer
For i = 1 To 15
ListView1.Items.Add(ds.Tables("Spring04").Rows(i).Item(1))
ListView1.Items(0).SubItems.Add(ds.Tables("Spring04").Rows(i).Item(0))
Next i
 
ListView1.Items(0).SubItems.Add(ds.Tables("Spring0 4").Rows(i).Item(0))

ListView1.Items().SubItems.Add(ds.Tables("Spring04").Rows(i).Item(0))

Also I think I get what jmcilhinney is saying... If you are creating an item as (1) wouldnt the subitems you are creating also want to be from ListView1.Items(1) ? im not sure, but worth a punt ;-)
 
So would you put the (i) through out the whole procedure...

As you are looping through each of the records, placing a number would be bad right? as if you are constricting it.
 
I'm assuming that you're adding an item and a subitem for each row, so for row 0 you add an item, which is then item 0, and you add a subitem. Then you add an item for row 1, which is then item 1, and you add a subitem, and so on. So the index of the ListView item corresponds to the index of the DataRow, which also corresponds to the loop index "i".
 
Well its not me thats doing it but your answer corresponds to my question perfectly =)

Im just glad I understand more and more every day ;-)

I think starman could also use;
VB.NET:
[COLOR=#0000ff]For[/COLOR][SIZE=2] i = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] ds.Tables("Spring04").Rows.Count -1
ListView1.Items.Add(ds.Tables("Spring04").Rows(i). Item(i))
ListView1.Items(i).SubItems.Add(ds.Tables("Spring04").Rows(i).Item(i))
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] i
[/SIZE]
yes?
 
Ah, lost track of who's who. Yes, that would do the trick. One thing I always recommend though: whenever you are using the Length of an array or the Count of a collection to set the end point of a For loop you should ALWAYS include the "Step 1" as well. That's because if you don't and the array/collection is empty you will get an exception thrown. With no step specified, your loop will try to go from 0 to -1 (if Count is zero the Count -1 is -1), so it will try to access the item/element at index 0 and throw an exception when there isn't one there. If you specify a positive step and the end point is less than the start then the loop will not execute at all and you'll get no exception.
 
Thanks all for your assistance. I'm preparing for taking the 070-293 exam on Monday these last few days, but once I'm through that I will read though the replies, and let you know how I get on. many thanks again..
 
hey your welcome and good luck with the exam :)
 
Back
Top