ListView Problems

shireenrao

Active member
Joined
Jun 18, 2003
Messages
31
Location
Boston
Hello
I am creating listView in a procedure -

Dim lvi As ListViewItem

Create a new ListView control.

Dim listView1 As New ListView()
ListView1.Bounds = New Rectangle(New Point(10, 10), New Size(400, 200))

Set the view to show details.
ListView1.View = View.Details
Allow the user to edit item text.
ListView1.LabelEdit = True
Allow the user to rearrange columns.
ListView1.AllowColumnReorder = True
Select the item and subitems when selection is made.
listView1.FullRowSelect = True
Display grid lines.
listView1.GridLines = True
Sort the items in the list in ascending order.
listView1.Sorting = SortOrder.Ascending
listView1.Columns.Add("User Id", -2, HorizontalAlignment.Left)
listView1.Columns.Add("User Name", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Last Name", -2, HorizontalAlignment.Left)
listView1.Columns.Add("First Name", -2, HorizontalAlignment.Left)
listView1.Columns.Add("Location", -2, HorizontalAlignment.Left)

Try

Dim myConnection As New OracleConnection(myConnString)
Dim myCommand As New OracleCommand(mySelectQuery, myConnection)
myConnection.Open()

Dim myReader As OracleDataReader
myReader = myCommand.ExecuteReader()
Dim strText As String

While myReader.Read
lvi = New ListViewItem()
lvi.Text = myReader("USER_ID")
lvi.SubItems.Add(myReader("USERNAME"))
lvi.SubItems.Add(myReader("LASTNAME"))
lvi.SubItems.Add(myReader("FIRSTNAME"))
lvi.SubItems.Add(myReader("LOCATION"))
listView1.Items.Add(lvi)
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try


Add the ListView to the control collection.
Me.Controls.Add(ListView1)

Now when I try to use ListView1 from another function or procedure, I cant. I know why i cant.. its because Its not declared and is being created dynamically in the previous procedure. So when I created a listView on the form, and removed the new declaration from the procedure for the listview, I cant see the items I am adding properly. I just get one row, no columns, and the one row has two data from the first column.

What am I doing wrong.
Thank you in advance
 
Correction

Hi

A small Correction in the previous post. I am getting all the data from column one in row one.

Thank you
 
If you want to create ListView at runtime, you need to declare it in your Form globally (that is, at the top of the Form class). If you declare it in a procedure, it will go out of scope and you wont be able to access it.

Why arent you just placing the ListView control on the form at design time, anyway?
 
Hi VolteFace
I am declaring the listview globaly now. It works but I would be happy to put it in my form during design time. But as I mentioned in my post previously, I get a messed up output.
 
When you put your ListView on the Form, you should add all of your columns at design time using the built in Collection editor in the Properties window (Click the ... beside the Columns property).

Also, where are you putting this code (that you pasted above)?
 
I am putting the code in form_load procedure.
I also figured out why I was having the problem. My listView view property was set to largeIcons, instead of details.
It works fine now.

Thank you for your help VolteFace
 
Hi VolteFace
I had a quick question about listviews again. I am trying to select a row and get the data, so I can edit that info in another form. This I can do. If I click on my edit button without making a selection, atleast the first time I can catch me error. The second time, I am editing the previous selection if I dont make a selection in the listview. any Ideas

Thanks
 
Hi
Please disregard my last post. I just had to do a ListView1.SelectedItems().Clear()

To clear the selectedItems collection.

Sorry about that
 
Back
Top