Solved

Find Index/Current Row of DataSet

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

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

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

20 Experts available now in Live!

Get 1:1 Help Now