- 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 ...
- 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 ...
- 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 ...
- 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 ...
- In this tutorial we will be creating a CSS3 Buttons using HTML5 Actually in creating a button in CSS3 is the most easiest way than creating a button using the Adobe Photoshop in a short coding you will be able to create a clean and a good quality button using the ...
- 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 ...
- Requirements: Visual Studio 2010 SAP Crystal Report for Visual Studio 2010 Level: Advance Concept: I have been using Crystal Report for so long, and it has been my partner in generating valuable reporting for my application projects. Crystal Reports provides its latest update from their website. I design my Reports to print in a non-standard paper ...
- How to do Image Processing using AForge with COM+ Object Requirements: Visual Studio 2005, 2008, 2010 Programming Level: Advance Language: Visual Basic.Net AForge.dll(v 18.104.22.168) AForge.Imaging.dll(22.214.171.124) AForge.Math.dll(126.96.36.199) Project references AForge AForge.Imaging AForge.Imaging.Filters AForge.Math.Geometry System.Collections.Generic System.Drawing System.Drawing.Imaging System.Reflection Scenario Microsoft Visual Basic 6 as we all know is the ancestor of Visual Basic.Net the current versions of Visual Studio is 2012 upon this writing, and still due to the ...
- Creating a true transparent image that supports GIF Requirements: Visual Studio 2005, Visual Studio 2008, Visual Studio 2010 Programming Level: Advance Language: Visual Basic.Net Scenario: Way back in visual basic 6.0 when i tried to use an image and I designed a GIF image with transparent background color, and I could not let the background ...
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