Solved

Find Index/Current Row of DataSet

Posted on 2006-10-22
2
410 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

17 Experts available now in Live!

Get 1:1 Help Now