Question MultiRows ListView problem

Sameer

New member
Joined
Nov 3, 2010
Messages
4
Programming Experience
Beginner
guys.. m new to this site n newbie with vb.net language also.
trying to display my data table from Microsoft access database via oledb connection.
the success in following code i got is the program is currently able to display only one row from the table. m not able to display more than 1 row. the error is-"Cannot add or insert the item '2' in more than one place. You must first remove it from its current location or clone it. Parameter name: item"

plz can any one solve my problem??

Code-

Imports System.Data.OleDb

Public Class Form1

Dim strConnection As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "E:\RealEstateManagerDB.mdb")
Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConnection)
Dim da As New OleDb.OleDbDataAdapter("select * from agent", cn)
Dim ds As New DataSet
Dim dt As New DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim da As New OleDb.OleDbDataAdapter("select * from agent", cn)
Dim MaxRows As Integer
Dim i As Integer
Dim ListItem = New ListViewItem()


da.Fill(ds, "agent")
ListView.View = View.Details
MaxRows = ds.Tables("agent").Rows.Count

For i = 1 To MaxRows - 1

ListView.Columns.Add("Agent ID", 80, HorizontalAlignment.Left)
ListView.Columns.Add("First Name", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Last Name", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Address", 80, HorizontalAlignment.Left)
ListView.Columns.Add("State", 80, HorizontalAlignment.Left)
ListView.Columns.Add("City", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Zip", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Phone", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Mobile", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Fax", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Email ID", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Other Info", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Comments", 80, HorizontalAlignment.Left)

ListItem.Text = ds.Tables("agent").Rows(i).Item(0)
ListItem.SubItems.Add(ds.Tables("agent").Rows(i).Item(1))
ListItem.SubItems.Add(ds.Tables("agent").Rows(i).Item(2))
ListView.Items.Add(ListItem)


Next i
'cn.Close()
End Sub

Private Sub ListView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView.SelectedIndexChanged


End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Me.Close()
End Sub
 
You really need to loop only adding the items, add the columns first. For your items loop create a new ListViewItem for each, ie within the loop.
 
Thnx allot for showing me my mistake... a real rookie mistake.
The suggestion above has solved my problem. here s my final code-

Imports System.Data.OleDb

Public Class Form2
Dim strConnection As String = ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & "E:\RealEstateManagerDB.mdb")
Dim cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(strConnection)
Dim da As New OleDb.OleDbDataAdapter("select * from agent", cn)
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


ListView.Columns.Add("Agent ID", 80, HorizontalAlignment.Left)
ListView.Columns.Add("First Name", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Last Name", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Address", 80, HorizontalAlignment.Left)
ListView.Columns.Add("State", 80, HorizontalAlignment.Left)
ListView.Columns.Add("City", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Zip", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Phone", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Mobile", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Fax", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Email ID", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Other Info", 80, HorizontalAlignment.Left)
ListView.Columns.Add("Comments", 80, HorizontalAlignment.Left)
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim da As New OleDb.OleDbDataAdapter("select * from agent", cn)
Dim MaxRows As Integer
Dim ListItem = New ListViewItem()

da.Fill(ds, "agent")
ListView.View = View.Details
MaxRows = ds.Tables("agent").Rows.Count

'Dim counter As Integer = 0
For i As Integer = 0 To MaxRows - 1
'counter += 1

With ds.Tables("agent").Rows(i)

Dim str(13) As String
Dim itm As ListViewItem
str(0) = ds.Tables("agent").Rows(i).Item(0).ToString
str(1) = ds.Tables("agent").Rows(i).Item(1).ToString
str(2) = ds.Tables("agent").Rows(i).Item(2).ToString
str(3) = ds.Tables("agent").Rows(i).Item(3).ToString
str(4) = ds.Tables("agent").Rows(i).Item(4).ToString
str(5) = ds.Tables("agent").Rows(i).Item(5).ToString
str(6) = ds.Tables("agent").Rows(i).Item(6).ToString
str(7) = ds.Tables("agent").Rows(i).Item(7).ToString
str(8) = ds.Tables("agent").Rows(i).Item(8).ToString
str(9) = ds.Tables("agent").Rows(i).Item(9).ToString
str(10) = ds.Tables("agent").Rows(i).Item(10).ToString
str(11) = ds.Tables("agent").Rows(i).Item(11).ToString
str(12) = ds.Tables("agent").Rows(i).Item(12).ToString
itm = New ListViewItem(str)
ListView.Items.Add(itm)


End With

Next

cn.Close()
End Sub
End Class


Thanks again for help.
 
One more thing you should do here is to call lw.BeginUpdate before the loop and lw.EndUpdate afterwards.

When posting code in these forums you should use the code box for readability.
 
one more thing. i need to search for a record from table n display ot in list view. the search ID or Name is to be accepted from textBox.
how can i get that??
m stucked in one part.
when i m using following query-(oledb connection)

select * from agent where agent_id='" & txtid.text & "'

how to display that record in list box??
i mean how to get index of search item/record from access table in the code?
hope u got what i mean to say.
 
sorry my above post was totally Useless.. i have figured out my issue by my own. if any one wants i can post my code. it is about search record from database n display it into the listView.(id wise,name wise)
 
Back
Top