3
Contributors
6
Replies
8
Views
5 Years
Discussion Span
Last Post by Ramesh Genwa
0

See if this helps.

Dim lvItem As New ListViewItem(TextBox1.Text) '// declare new ListView item and set TextBox.Text as item.Text.
        With lvItem.SubItems
            .Add(ComboBox1.Text) '// SubItem 1 for Column 2.
            .Add(ComboBox2.Text) '// SubItem 2 for Column 3.
        End With
        ListView1.Items.Add(lvItem) '// add item to ListView.
0

hi thanks codeorder what if i want to cancel the transaction? it should also be cleared out(the items on listview)

0

hi i know that it Listview1.items.clear() is one of the best way to do this. do you have any other way to refresh or clear the items?

0

If you need to clear all the items, then ListView1.Items.Clear() is the appropriate way to do it.
.Although, you can use a For/Next loop to clear items as well.

With ListView1
            For i As Integer = .Items.Count - 1 To 0 Step -1 '// loop backwards thru ListView.
                .Items.Remove(.Items.Item(i)) '// remove item from ListView.
            Next
        End With
        '// looping backwards will not affect the "i" in the loop as looping forwards does when deleting items.

If you need to remove only a single item, then see if this helps.

With ListView1
            If .Items.Count = 0 Then Exit Sub
            If Not .SelectedItems.Count = 0 Then '// check if item is selected.
                .Items.Remove(.SelectedItems(0))
            Else
                MsgBox("Please select an item to remove from ListView.")
            End If
        End With

.Or this.

ListView1.Items.Remove(ListView1.Items.Item(2)) '// remove 3rd item from ListView.

Edited by codeorder: n/a

0

thanks codeorder! I will try this next time to my application. For now I'm gonna close the thread :)

0

Public Class Form1
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim z As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Administrator\My Documents\ignou1.mdb"
cn.Open()
z = 1
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sql As String
Dim x As Integer = 0
sql = "select * from assign"
rs.Open(sql, cn)
Do While Not rs.EOF
ListView1.Items.Add(rs.Fields(0).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(1).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(2).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(3).Value)
ListView1.Items(x).SubItems.Add(rs.Fields(4).Value)
rs.MoveNext()
x = x + 1
Loop
rs.Close()
End Sub

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
TextBox1.Text = e.Item.Text
TextBox2.Text = e.Item.SubItems(1).Text
TextBox3.Text = e.Item.SubItems(2).Text
TextBox4.Text = e.Item.SubItems(3).Text
TextBox5.Text = e.Item.SubItems(4).Text
z = z + 1
End Sub

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

End Sub
End Class

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.