Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding a row in a DataGridView without Looping?

Posted on 2014-11-04
2
Medium Priority
?
760 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
2 Comments
 
LVL 35

Accepted Solution

by:
it_saige earned 2000 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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

926 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