Solved

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

Posted on 2006-10-19
7
277 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
VB.net Open Folder in Windows Explorer 3 59
VB.NET 2008 Publish Error 2 34
Winform Module - What is the ASP.Net equiv 2 28
VB.net Progress Bar - Maximum Value too large 2 16
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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