DataView Find Problem!!

Posted on 2006-04-17
Last Modified: 2008-01-09

I have a dataview with a football league table, already sorted showing the position of each team in the league. I want to find a specific team in the league table to find out which row they are in within my dataview. I'm trying the 'Find' method but keep coming up the a "Expecting 3 value(s) for the key being indexed, but received 1 value(s)" error. I have set a Primary Key on one value (I have a column called 'Pos' relating to a numeric position in the league), but can't get it to work - I want to output the position of my specific team (MySelectedTeam) into the integer value 'x'. Here is the code:

Dim DataView As Data.DataView
DataView = dsMonth.Tables(0).DefaultView
dsMonth.Tables(0).PrimaryKey = New Data.DataColumn() {dsMonth.Tables(0).Column("pos")}

DataView.Sort = "[points] DESC,[totalgd] desc,[teamname]"
Dim x As Integer = DataView.Find("MySelectedTeam")
Question by:ewangardner
    LVL 11

    Expert Comment

    Looks to me

    DataView.Find finds a row in the DataView by the specified sort key value.
    You are using 3 keys to sort but one for selecting it


    Author Comment

    Ok, that makes sense, but I need to sort by three keys but can only search one (Teamname) because I won't know the other two values i.e. Points and Goal difference. How would I do that?
    LVL 10

    Expert Comment

    It would often be useful to be able to sort by more than one column in a DataGrid. .Net has no built-in way to sort by multiple columns, but this article shows one way that some extra code can make this "multi-sorting" possible.

    This is what you want.

    LVL 10

    Expert Comment


    This is source code link

    previous was for live DEMO

    LVL 11

    Accepted Solution

    Try this.

    step1. Sort the dataview only by team name.
    step2. Now find the rows by teamname using FindRows method.
    step3. Now with in these Rows you can again provide your rest of filters and select the first row.

    That will provide you desired result.


    Author Comment

    Thanks for all your help guys. Managed to do it after ALL DAY trying. Here's how.

    jatinderalagh was correct initially because I had set three keys and then only tried to perform the find on one category. This is fine, but I need to filter by three for the league to be in the correct order.

    So, I can use the Dataview.Find on one category i.e. teamname, the one I know, but I can't use find when using all three category sorts because Find requires that you specify exactly what you're looking for for all three. I can't know the other two categories (points and goal difference) and thus I can't use Find.

    However, jatinderalagh's second post gave me the inspiration for the solution. I can perform the Find on teamname alone, find out what the values are for points and goal difference, then sort and find using all three categories, inputting the values I found, to discover the row index of my selected team as it would appear in the correct league order.

    Confusing I know, but here is my final code which should clear up the confusion.

            'Sort by teamname alone and find my selected team

            DataView.Sort = "teamname"
            Dim p = DataView.Find("Thurrock")
            'This is the row where MySelectedTeam appears at the moment...

            temprow = DataView(p).Row
            '...and I can use this row index go get the other values I need

            temp_points = temprow("points")
            temp_gd = temprow("totalgd")
            'Now I can sort with all three values to give me the correct league order

            DataView.Sort = "points desc, totalgd desc, teamname"
            'I now assign all the desired Find search values (including the ones I didn't know)
            'into the vals array

            Dim vals(2) As Object
            vals(0) = temp_points
            vals(1) = temp_gd
            vals(2) = "Thurrock"
            'Here I perform the Find, and I get the row index I need for my team

            Dim x As Integer = DataView.Find(vals)

    jatinderalagh provided the inspiration, I'm going to award the points to him, but thanks for the other suggestions.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    734 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

    19 Experts available now in Live!

    Get 1:1 Help Now