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

x
?
Solved

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

Posted on 2006-10-19
7
Medium Priority
?
281 Views
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.

Thanks!
0
Comment
Question by:kerryw60
[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
  • 3
  • 3
7 Comments
 
LVL 29

Expert Comment

by:Nightman
ID: 17769559
Try
Dim RowFind As DataRow = dtFleet.Rows.Find("FleetID" = x.ToString())
0
 
LVL 6

Author Comment

by:kerryw60
ID: 17770906
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.

Thanks!
0
 
LVL 10

Expert Comment

by:gangwisch
ID: 17771204
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
next
0
Technology Partners: 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!

 
LVL 6

Author Comment

by:kerryw60
ID: 17773167
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)
     dr(i)("FleetID")
Next


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!!
THANKS!!
0
 
LVL 29

Expert Comment

by:Nightman
ID: 17779949
Dim foundRow As DataRow = myTable.Rows.Find(x)
   ' Print the value of column 1 of the found row.
   If Not (foundRow Is Nothing) Then
     MessageBox.Show("Found")
   Else
     MessageBox.Show("NotFound")
   End If
0
 
LVL 29

Accepted Solution

by:
Nightman earned 2000 total points
ID: 17780034

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"
dtFleet.Columns.Add(myColumn)
' 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
0
 
LVL 6

Author Comment

by:kerryw60
ID: 17784746
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:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_22033635.html

THANKS!!
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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

704 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