Solved

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

Posted on 2006-10-19
7
273 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

15 Experts available now in Live!

Get 1:1 Help Now