Solved

Finding a row in a DataGridView without Looping?

Posted on 2014-11-04
2
575 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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