Solved

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

Posted on 2006-10-19
7
269 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
  • 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 500 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

9 Experts available now in Live!

Get 1:1 Help Now