How to search for a value in Datagridview

Post to Twitter

How to Search values in Datagridview

  • Requirements: Microsoft Visual Studio 2005, 2008, 2010
  • Language: Microsoft Visual Basic.Net
  • Level: Moderate


The Datagridview Control of MS Visual Studio IDE plays an important role in programming, The Datagridview in Visual Studio.Net is a combinations of a Datagrid and a Listview Functionalities derived from vb6 controls. Compare to vb6 Listview and datagrid, the datagridview is a collection meaning it is a collection of rows and a collection of columns, since that it is a collection therefore you can iterate to its rows and columns. In this article, I am going to show how to use these datagridview functionalities to locate for a duplicate entry before inserting it to the datagridview.


Create a new project in Visual Studio, then add a windows form or use the default form associated with the creation of the new project. Add a Datagridview control, a button and two textboxes on the form. Name the controls as Follows: frmTest, dtgData, btnAddData, txtData1 and txtData2, Add two Columns in the datagridview by right clicking the datagridview and add two columns, you can name it, Data1 and Data2 respectively.

The Codes:

Add the following codes as shown below:

Public Class frmTest
''' <summary>
''' Searches an item on datagridview.
''' </summary>
''' <param name="dtg">The Datagridview to be searched.</param>
''' <param name="ColumnName">The Column to be search upon.</param>
''' <param name="ValueToSearch">The Value to be searched.</param>
''' <returns>Returns a boolean value</returns>
''' <remarks>By Nolan Sunico</remarks>
Public Shared Function SearchGridValue(ByVal dtg As DataGridView, ByVal ColumnName As String, ByVal ValueToSearch As String) As Boolean
   Dim Found As Boolean = False
        Dim StringToSearch As String = ""
        Dim ValueToSearchFor As String = ValueToSearch.Trim.ToLower
        Dim CurrentRowIndex As Integer = 0
            If dtg.Rows.Count = 0 Then
                CurrentRowIndex = 0
                CurrentRowIndex = dtg.CurrentRow.Index + 1
            End If
            If CurrentRowIndex > dtg.Rows.Count Then
                CurrentRowIndex = dtg.Rows.Count - 1
            End If
            If dtg.Rows.Count > 0 Then
                For Each gRow As DataGridViewRow In dtg.Rows
                    StringToSearch = gRow.Cells(ColumnName).Value.ToString.Trim.ToLower
                    If StringToSearch.Contains(ValueToSearchFor) Then
                        Dim myCurrentCell As DataGridViewCell = gRow.Cells(ColumnName)
                        dtg.CurrentCell = myCurrentCell
                        Found = True
                    End If
                    If Found Then
                        Exit For
                    End If
            End If
            If Not Found Then
                If dtg.Rows.Count > 0 Then
                    Dim myFirstCurrentCell As DataGridViewCell = dtg.Rows(0).Cells(ColumnName)
                    dtg.CurrentCell = myFirstCurrentCell
                End If
            End If
        Catch ex As Exception
            MsgBox("Error: " & ex.Message, MsgBoxStyle.Information)
        End Try
        Return Found
End Function

'********************** How to use **********************
Private Sub btnAddData_Click(ByVal sender As System.Object,
   ByVal e As System.EventArgs) Handles btnAddData.Click
	If SearchGridValue(Me.dtgData, "Data1", Me.txtData1.Text) Then
	    MessageBox.Show(Me.txtData1.Text & " Already in the list!")
	    Exit Sub
	End If
	dtgData.Rows.Add(Me.txtData1.Text, Me.txtData2.Text)
	Catch ex As Exception
   End Try
End Sub
End Class


This function can be use to search first for a value in a datagridview before inserting it, or you an use this function to locate for a value in datagrdivew control and return the row associated with the search. This is the better code than what I created earlier.
VN:F [1.9.6_1107]
Rating: 9.3/10 (24 votes cast)
VN:F [1.9.6_1107]
Rating: +6 (from 6 votes)

How to search for a value in Datagridview, 9.3 out of 10 based on 24 ratings

Post to Twitter

Keywords: , , , , , , , ,

4 comments on “How to search for a value in Datagridview

  1. Patience on said:

    Extremely helpful airtcle, please write more.

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  2. su arıtma on said:

    Awesome post ! Thank you for, writing on this blog mate! Ill message you soon! I did not realise that!

    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: 0 (from 0 votes)
  3. Pingback: personal creations promotion or source code

  4. bhaskar on said:


    VA:F [1.9.6_1107]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.6_1107]
    Rating: -3 (from 3 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *


151,340 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.