none
update in listview RRS feed

  • Question

  • hi

    I want to add data in listview1 using textbox1. But first the program must check if the data provided in textbox already exists than update  otherwise add in listview1. Based on the ProductName column.


    MUHAMMAD ANZAR E-mail : muhammadanzar@hotmail.com Mobile # :0092-3215096959

    Tuesday, November 19, 2019 2:35 PM

Answers

  • Try the following, in this case I searched on the sub item. if the first column has product name use 

    listview1.Items(index).Text where index is in a for-next rather than a for-each.

    Private Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    	Dim Found As Boolean = False
    	For Each lvi As ListViewItem In ListView1.Items
    
    		If lvi.SubItems(1).Text = TextBox1.Text Then
    			Found = True
    			Exit For
    		End If
    	Next lvi
    
    	If Found Then
    	   ' item exists
    	Else
    	   ' item does not exists
    	End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 19, 2019 3:26 PM
    Moderator

All replies

  • Try the following, in this case I searched on the sub item. if the first column has product name use 

    listview1.Items(index).Text where index is in a for-next rather than a for-each.

    Private Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    	Dim Found As Boolean = False
    	For Each lvi As ListViewItem In ListView1.Items
    
    		If lvi.SubItems(1).Text = TextBox1.Text Then
    			Found = True
    			Exit For
    		End If
    	Next lvi
    
    	If Found Then
    	   ' item exists
    	Else
    	   ' item does not exists
    	End If
    End Sub


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 19, 2019 3:26 PM
    Moderator
  • Hi

    Here is a much less compact offering.. It is a stand alone example and just needs a blank Form1 as all controls are created dynamically.

    ' blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim WithEvents listView1 As New ListView()
    	Dim TB As New TextBox
    	Dim WithEvents b1 As New Button
    	Dim rand As New Random
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		DoubleBuffered = True
    		CreateMyListView()
    
    		With TB
    			.Location = New Point(20, listView1.Bottom + 8)
    			.Font = New Font(TB.Font.FontFamily, 14)
    			.Width = 160
    		End With
    
    		With b1
    			.Text = "Click"
    			.Location = New Point(TB.Right + 10, TB.Top)
    		End With
    
    		Controls.AddRange({listView1, b1, TB})
    	End Sub
    	Private Sub CreateMyListView()
    		' some random Product Names
    		Dim pn() As String = {"Bikes", "Prams", "Dolls", "Games", "Teddy Bears", "Toy Cars", "Robots", "Dynasaurs"}
    		' Create new ListView.
    		With listView1
    			.Bounds = New Rectangle(New Point(10, 10), New Size(500, 300))
    			' Set to details.
    			.View = View.Details
    			' Allow item edit.
    			.LabelEdit = True
    			' Allow rearrange columns.
    			.AllowColumnReorder = True
    			' check boxes.
    			.CheckBoxes = True
    			' Select whole row.
    			.FullRowSelect = True
    			' Show grid lines.
    			.GridLines = True
    			' Sort ascending.
    			.LabelWrap = True
    			.Sorting = SortOrder.Ascending
    			listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    			.Columns.Add("Product Name", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    			.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 5", -2, HorizontalAlignment.Center)
    			.Columns.Add("Column 6", -2, HorizontalAlignment.Center)
    
    			' make some test data
    			For itm As Integer = 0 To 9
    				Dim item As New ListViewItem(rand.Next(999, 9999).ToString, rand.Next(0, 2))
    				item.Checked = True
    				item.SubItems.Add(pn(rand.Next(0, pn.Length)))
    				item.SubItems.Add((rand.NextDouble * rand.Next(200, 299)).ToString("0.0000"))
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.00"))
    				item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.0"))
    				item.SubItems.Add("abcdefghijkl" & vbCrLf & vbCrLf & "mnopqrstuvwxyz")
    				.Items.Add(item)
    			Next
    
    			' Create ImageList objects.
    			Dim imageListSmall As New ImageList()
    			Dim imageListLarge As New ImageList()
    
    			' Initialize ImageList
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42small.png"))
    			imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43small.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42.png"))
    			imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43.png"))
    
    			'Assign ImageList to ListView.
    			.LargeImageList = imageListLarge
    			.SmallImageList = imageListSmall
    		End With
    	End Sub
    
    	Dim htInfo As ListViewHitTestInfo
    	Dim intLast As Integer = 0
    	Private Sub LvMain_MouseMove(sender As Object, e As MouseEventArgs) Handles listView1.MouseMove
    		If Not listView1.View = View.Details Then
    			listView1.Items(intLast).BackColor = listView1.BackColor
    		Else
    			htInfo = listView1.HitTest(e.Location)
    
    			If htInfo.Item Is Nothing Then Exit Sub
    			If htInfo.Item.Index <> intLast Then
    				listView1.Items(intLast).Selected = False
    
    				htInfo.Item.Selected = True
    
    				intLast = htInfo.Item.Index
    			End If
    		End If
    	End Sub
    	Private Sub B1_Click(sender As Object, e As EventArgs) Handles b1.Click
    		If TB.Text = Nothing Then Exit Sub
    		Dim tb2 As String = TB.Text.ToLower
    		Dim found As String = Nothing
    		For i As Integer = 0 To listView1.Items.Count - 1
    			If listView1.Items(i).SubItems(1).ToString.ToLower.Contains(tb2) Then
    				found = listView1.Items(i).SubItems(0).ToString
    				Exit For
    			End If
    		Next
    
    		If Not found = Nothing Then
    			MessageBox.Show(tb2 & " found in item " & found)
    		Else
    			Dim item As New ListViewItem(rand.Next(999, 9999).ToString, rand.Next(0, 2))
    			item.Checked = True
    			item.SubItems.Add(tb2)
    			item.SubItems.Add((rand.NextDouble * rand.Next(200, 299)).ToString("0.0000"))
    			item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.00"))
    			item.SubItems.Add((rand.NextDouble * rand.Next(300, 399)).ToString("0.0"))
    			item.SubItems.Add("abcdefghijkl" & vbCrLf & vbCrLf & "mnopqrstuvwxyz")
    			listView1.Items.Add(item)
    		End If
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, November 19, 2019 3:44 PM
  • Here is another way 

    Private Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    	Dim foundItem As ListViewItem = ListView1.FindItemWithText(searchBox.Text, False, 0, False)
    	If foundItem IsNot Nothing Then
    		MessageBox.Show($"{foundItem.Text} already exists")
    	End If
    End Sub

    Documentation

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-add-search-capabilities-to-a-listview-control


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, November 19, 2019 3:58 PM
    Moderator
  • Hi,

    Do you resolve the issue? I think the above reply can provide you with a solution, have you tried it?

    If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 20, 2019 7:07 AM
    Moderator