We help IT Professionals succeed at work.

How to locate a row in a datatable and get the row number.

kerryw60 asked
Medium Priority
Last Modified: 2010-04-23
The user selects a value from a combobox and I would like to use that value to find a particular row in a datatable.

I have the value of the field I'm looking for.
Need to find row in my datatable where FleetID = x
AND get the row number when found.

Was trying to do this to locate row:

Dim RowFind As DataRow = dtFleet.Rows.Find("FleetID" = x)

I get an error: InvalidCastException - Conversion from string "FleetID" to type 'Double' is not valid.

Watch Question

Dim RowFind As DataRow = dtFleet.Rows.Find("FleetID" = x.ToString())


I had tried converting to a string before and got an error. Tried again, still got error regarding Primary Key.

Did not realize I had to set a Primary Key in my DataTable. I've been fooling around with trying to get that to work, but not having much luck.  Getting late for me, will try again in the morning.

dim dt as datatable
dim dr() as datarow = dt.select("Fleetid=" & x.tostring)
for i as integer=0 to dr.getupperbound(0)
dr(i)("fleetid") will get you any field you need


Sorry, but cannot get the code to work.  I'm afraid I don't quite get what you're trying to do with:

For i As Integer = 0 To dr.GetUpperBound(0)

I receive an error on "dr(i)" that "Expression is not a Method"

I simply need to get the Row Position Number where my FleetID=x

I really appreciate the help!!

Dim foundRow As DataRow = myTable.Rows.Find(x)
   ' Print the value of column 1 of the found row.
   If Not (foundRow Is Nothing) Then
   End If

To create the primary Key (assumes that FleetID is your primary key)
Dim keys(0) As DataColumn
Dim myColumn  As DataColumn
myColumn = New DataColumn()
myColumn.DataType = System.Type.GetType("System.Int32")
myColumn.ColumnName= "FleetID"
' Add the column to the array.
keys(0) = myColumn
dtFleet.PrimaryKey = keys

Alternatively (I haven't tried this, so it is theoretical)

Dim keys(0) As DataColumn
keys(0) = dtFleet.Columns("FleetID")
dtFleet.PrimaryKey = keys

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Great Nightman!!

This works:

Dim keys(0) As DataColumn
keys(0) = dtFleet.Columns("FleetID")
dtFleet.PrimaryKey = keys

Was very close to what I was trying to do, but I just didn't quite have it. THANKS!!
This gets me MOST of the way there.
Now that I've located the row, I need to get the row position/index of that row.
Giving you the points here and re-asking the other part of my question in the following location for another 500 points:


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.