Solved

Find Index/Current Row of DataSet

Posted on 2006-10-22
2
418 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
[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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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