listview question

S

SA

My listview has 3 columns i.e. name, populaiton, and country code in order.
I use following code to insert data into listview and I find that I can't
assign which subitem related to related field. e.g. assign population to
column2. In vb 6, I can do it by "subitems(i). How about in .net? thx.

i = 0
While MyDataAdapter.Read
ListView1.Items.Add("")
ListView1.Items(i).Text = CStr(MyDataAdapter("name"))
ListView1.Items(i).SubItems.Add(MyDataAdapter("population"))
ListView1.Items(i).SubItems.Add(MyDataAdapter("CountryCode"))
i = i + 1
End While
 
K

Ken Tucker [MVP]

Hi,

Dim strConn As String
Dim conn As SqlConnection
Dim drCustomer As SqlDataReader
Dim cmd As SqlCommand

strConn = "Server = " + Environment.MachineName + ";"
strConn += "Database = NorthWind;"
strConn += "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)
cmd = New SqlCommand("Select * from Customers", conn)
conn.Open()

Dim chID As New ColumnHeader
chID.Text = "Customer ID"
chID.Width = 70

Dim chCompany As New ColumnHeader
chCompany.Text = "Company Name"
chCompany.Width = 200

Dim chContact As New ColumnHeader
chContact.Text = "Contact Name"
chContact.Width = 150

ListView1.Columns.Add(chID)
ListView1.Columns.Add(chCompany)
ListView1.Columns.Add(chContact)

ListView1.View = View.Details

drCustomer = cmd.ExecuteReader
Do While drCustomer.Read
Dim lvi As New
ListViewItem(drCustomer.Item("CustomerID").ToString)
lvi.SubItems.Add(drCustomer.Item("CompanyName").ToString)
lvi.SubItems.Add(drCustomer.Item("ContactName").ToString)
ListView1.Items.Add(lvi)
Loop
conn.Close()

Ken
 
S

SA

Firstly, thx but the order must be same in your code.
example, the column order is CustId, CompanyName, and Contact
During add record, I set a wrong order and the result is that contact name
data is added to column 2 i.e. Company Name.

ListViewItem(drCustomer.Item("CustomerID").ToString)
lvi.SubItems.Add(drCustomer.Item("ContactName").ToString)
lvi.SubItems.Add(drCustomer.Item("CompanyName").ToString)
ListView1.Items.Add(lvi)

Is any mistake in my view? thx.
 
K

Ken Tucker [MVP]

Hi,

Yes you are right

Ken
-----------
SA said:
Firstly, thx but the order must be same in your code.
example, the column order is CustId, CompanyName, and Contact
During add record, I set a wrong order and the result is that contact name
data is added to column 2 i.e. Company Name.

ListViewItem(drCustomer.Item("CustomerID").ToString)
lvi.SubItems.Add(drCustomer.Item("ContactName").ToString)
lvi.SubItems.Add(drCustomer.Item("CompanyName").ToString)
ListView1.Items.Add(lvi)

Is any mistake in my view? thx.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top