- Did you know how to backup or restore MySQL Data? Requirements: MySQL Database Engine Programming Level: Moderate Language: MySQL Scenario: You want to backup your data at run-time without shutting down the mysql server (Binary Backup), in simply term you want to perform an online backup while other client is connected to server performing ...
- How to configure Oracle VM Virtual Box Requirements: Oracle VM Virtual Box Website: https://www.virtualbox.org Level: Advance Knowledge: Windows Installations, adminsitrations Scenario Most of the windows installed today are in windows 7 or higher, due to this most of the program that were smoothly running from previous versions of windows like WindowsXP, Windows ME and other ...
- Enhanced Entity Relationship Diagram (EER) Requirements: MySQL WorkBench 5.2 CE or later Language: SQL/Database Management System Level: Beginner Prerequisite: SQL Knowledge Definition: Entity Relationship Diagram or ER diagram is a data modeling method represented by symbols use to produce a relational database. A relational database is a database that conforms to relational model theory managed by ...
- Crystal Report in VS2010 Requirements: Visual Studio 2005, 2008, 2010 Programming Level: Moderate Language: Visual Basic.Net Project references CrystalDecisions.CrystalReports.Engine CrystalDecisions.ReportSource CrystalDecisions.Shared CrystalDecisions.Windows.Forms Scenario Crystal reports has been my partner since I have learned Programming. For me this is the best tool in creating professional looking reports that can even generate from easy to complex reporting. In this article I presume ...
- IIS-PHP Installations and Configurations Requirements: PHP 5.4.24, Microsoft Visual C++ 2008 Redistributable Package (x64) , PHP Manager for IIS 7, IIS Url rewrite 2.0 IIS Extension, Microsoft Drivers 3.2 for PHP v5.4 for SQL Server in IIS, MySQL WorkBench, Major Requirements:Oracle Virtual Machine or VMWare - Virtual Machine Web Server:Internet Information Server ...
- How to Check Alpha and numeric in a band of a string Requirements: Visual Studio 2005, 2008, 2010 Programming Level: Basic Language: Visual Basic.net Scenario I have been working to my new project recently for Inventory consolidation system, wherein it imports textfile format from other system for consolidation process. These Text file contains multiple column ...
- Creating a true transparent label. Requirements: Visual Studio 2010 Programming Level: Advance Language: Visual Basic.net Scenario: I have been looking for quite some time a control that can give me a true transparent label in which we can see the background color or object behind this control just like the old VB6 language, by just ...
- Loading xml from file to datagridview Requirements: Visual Studio 2005, 2008, 2010 Programming Level: Moderate Language: Visual Basic.Net Scenario An XML is stands for Extensible Markup Language, that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable . In a modern programming architecture xml plays an important ...
- Converting datagridview row data into datatable Requirements: Microsoft Visual Studio 2005/2008/2010 Programming Level: Advance Language: Microsoft Visual Basic.Net Scenario: Datagridview is an essential component of visual studio wherein you could populate data on it by either a databinding or manual inserting of data. Being a visual basic programmer for almost 8 years, I'd rather choose ...
How to search for a value in Datagridview
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 vb.net 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.
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 Try If dtg.Rows.Count = 0 Then CurrentRowIndex = 0 Else 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 Next 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 Try 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 MsgBox(ex.Message) 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.
How to search for a value in Datagridview,
Other reading this article are also reading these:
4 comments on “How to search for a value in Datagridview”
Pingback: personal creations promotion or source code