Solved

Find Index/Current Row of DataSet

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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