DataView Find Problem!!

Posted on 2006-04-17
Medium Priority
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
  • 2
  • 2
  • 2
LVL 11

Expert Comment

ID: 16468295
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

ID: 16468492
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

ID: 16468563
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.


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 10

Expert Comment

ID: 16468569

This is source code link


previous was for live DEMO

LVL 11

Accepted Solution

jatinderalagh earned 500 total points
ID: 16468737
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

ID: 16471338
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

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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