Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Finding a row in a DataGridView without Looping?

Posted on 2014-11-04
2
Medium Priority
?
734 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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

705 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