Solved

Finding a row in a DataGridView without Looping?

Posted on 2014-11-04
2
595 Views
Last Modified: 2014-11-04
I know the ComboBox has a FindString method to locate a specific value. Does the DataGridView (dgv) have anything like this functionality? Currently, I need to loop thru all the rows in a dgv and match a column value with a value that I am seeking. Just wondered if there was a more efficient/quicker method?

Thanks!
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40421576
A more efficient and quicker method involves using a BindingSource and making the datasource for the binding source a collection of the objects displayed on the DataGridView.  Then you could use the Find method on the BindingSource as long as your collection implements FindCore, e.g. -
Private objectList As New BindingList(Of YourObject)()
Private source As New BindingSource()

Public Sub Main()
	source.DataSource = objectList

	dataGridView1.DataSource = source
End Sub

Private Function GetIndexOfItemById(id As Integer) As Integer
	Return source.Find("Id", id)
End Function

Open in new window


However, the framework BindingList does not implement FindCore so a custom IBindingList class would have to be generated.  Here is some code from MSDN with regards to an IBindingList that supports FindCore():
Protected Overrides ReadOnly Property SupportsSearchingCore() As Boolean
	Get
		Return True
	End Get
End Property

Protected Overrides Function FindCore(prop As PropertyDescriptor, key As Object) As Integer
	' Get the property info for the specified property.
	Dim propInfo As PropertyInfo = GetType(T).GetProperty(prop.Name)
	Dim item As T

	If key IsNot Nothing Then
		' Loop through the items to see if the key
		' value matches the property value.
		For i As Integer = 0 To Count - 1
			item = DirectCast(Items(i), T)
			If propInfo.GetValue(item, Nothing).Equals(key) Then
				Return i
			End If
		Next
	End If
	Return -1
End Function

Open in new window


http://msdn.microsoft.com/en-us/library/aa480736.aspx

Example projects for both C# and VB.NET located here: http://www.microsoft.com/en-us/download/details.aspx?id=1631

Other than that you could use linq (behing the scenes linq will still loop through the rows).

-saige-
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40421590
I always forget about using the BindingSource. I've used it before and it works great!

Thanks saige!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question