Solved

Finding a row in a DataGridView without Looping?

Posted on 2014-11-04
2
494 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 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
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
Comment Utility
I always forget about using the BindingSource. I've used it before and it works great!

Thanks saige!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now