Pointing a currencyManager to a random position

parkerjnc

Member
Joined
Oct 12, 2007
Messages
10
Programming Experience
3-5
I have a large dataset that I want to navigate through in a somewhat random order.

Each record has an id field which does not necessarily correspond to the row number of the record. There is an array of a subset of the ids and I want to randomly select ids from this array and point the CM to the appropriate position for that record.

I can take the id and perform a Find to find the datarow that contains that id. How do I relate that then to the appropriate integer to set the CM.Position to?

Any help appreciated,
parkerjnc
 
What's the point of selecting a random ID and getting its position when you can just select a random position? Also, there shouldn't be a need to access a CurrencyManager directly in .NET 2.0 as you should be using a BindingSource:
VB.NET:
myBindingSource.Position = myRandom.Next(0, myBindingSource.Count)
 
Thanks for the info.

I could randomly pick a Position; however, I am trying to work with a subset of the records which is already known. This subset will get smaller and smaller as records are acted upon, so I was hoping to avoid jumping around multiple times till I found a record in the subset. Is it not worth the performance concern?

Also, what does having the BindingSource gain you over working directly with the CurrencyManager?
 
Here is a sample regarding selecting random unique items from a collection:
VB.NET:
Private r As New Random

Private Sub sample()
    [COLOR="DarkGreen"]'the selections[/COLOR]
    Dim l As New List(Of Integer)
    l.AddRange(New Integer() {1, 3, 5, 7, 9})
    [COLOR="darkgreen"]'pick a random item #1[/COLOR]
    Dim current As Integer = l(r.Next(0, l.Count))
    l.Remove(current)
    [COLOR="darkgreen"]'display the results[/COLOR]
    Display(current, l.ToArray)
    [COLOR="darkgreen"]'pick a random item #2[/COLOR]
    current = l(r.Next(0, l.Count))
    l.Remove(current)
    [COLOR="darkgreen"]'display the results[/COLOR]
    Display(current, l.ToArray)
    [COLOR="darkgreen"]'etc[/COLOR]
End Sub

Private Sub Display(ByVal current As Integer, ByVal remaining() As Integer)
    Dim tmp() As String = Array.ConvertAll(remaining, New Converter(Of Integer, String)(AddressOf IntStrConverter))
    MsgBox(String.Format("choosen: {0} remaining: {1}", current, String.Join(" ", tmp)))
End Sub

Private Function IntStrConverter(ByVal input As Integer) As String
    Return input.ToString
End Function
 
If you're starting with a subset of rows that has been created by some filter and that set may not have contiguous indexes then selecting IDs would be appropriate. If you're starting with all rows and removing a row from the list of possibilities once it has been selected then selecting by index would be appropriate.

The BindingSource is a one-stop-shop for data-binding, providing easy access to the functionality of a CurrencyManager, a DataView and more.
 
Back
Top