Solved

Find Index/Current Row of DataSet

Posted on 2006-10-22
2
416 Views
Last Modified: 2012-08-13
I have a populated DataTable (dtFleet) with FleetID as the Primary Key.

Based on user selection in a combo box, I am able to locate the row of my DataTable by:

Dim RowFind as DataRow = dtFleet.Rows.Find(x)

I realize I could return values of other fields here, but I already have a routine which populates text fields based on:

Dim RowPosition as Integer = 0

'I use the following for MoveFirst, MovePrevious, MoveNext, MoveLast buttons

Private Sub ShowCurrentRecord()
     If dtFleet.Rows.Count = 0 then
          me.ClearScreen()    'Sub clears screen if datatable is empty
          Exit Sub
     End if

     txtFleetName.Text = dtFleet.Rows(RowPosition)("FleetName").ToString()
     txtFleetAdd1.Text = dtFleet.Rows(RowPosition)("Add1").ToString()
     'etc
     'etc
     'Loads all fields
end sub


If I can just get the row position/index based on this other selection, I can use this same routine to populate the fields for the user.

SO, How do I return the row position/index into my RowPosition variable after locating the row in my DataTable which contains the selected Primary Key?



0
Comment
Question by:kerryw60
2 Comments
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 17785328
If you are on VB.NET 2005, you can use .IndexOf: see here

http://msdn2.microsoft.com/en-us/library/system.data.datarowcollection.indexof.aspx

Otherwise, I know of no way other than iterating through the rows to identify the row you want

    Dim Found As Boolean = False
    For i As Integer = 0 To myDataTable.Rows.Count-1
        If myDataTable(i)(myField) = x Then
            Found = True
            Exit For
        End If
    Next
    If Found Then
        myIndex = i
    End If

But you might like to consider amending yourShowCurrentRecord sub by adding a datarow as its argument

Private Sub ShowCurrentRecord(dr As DataRow)
     If dr = Nothing then
          me.ClearScreen()    'Sub clears screen if datatable is empty
          Exit Sub
     End if

     txtFleetName.Text = dr("FleetName").ToString()
     txtFleetAdd1.Text = dr("Add1").ToString()
     'etc
     'etc
     'Loads all fields
end sub

You could then pass the relevant row to it both when you wanted to move to a specific position (start, end, next, previous) as well as when you wanted to display a found record.

Roger
0
 
LVL 6

Author Comment

by:kerryw60
ID: 17792491
I'm using 2005.  Just seems odd that you can SET the index, but you cannot GET the index.

I've have spent many hours on this and can't find a solution anywhere. Tried iterating through the rows and that just wouldn't work for me either.

Took your advice and changed my ShowCurrentRecord. Unfortunately that was just one piece I had working and had to change. Took about 3 hours for me to update everything. I was just certain I was going to be able to get that index.  BUT, speding 3 hours changing everything and getting it to work is better than spending another 3 hours chasing a solution that may not exist.

Thanks for the suggestion. I really appreciate you taking the time to post it. Giving you the points.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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 …
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/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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