Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please sir, how can i implement the linq code below in a data bound DataGridView. My DataGridView is bound to a Dataset but I have one unbound column which I want to display the output postion from the linq code. My DataGridView look like this

Names Total score position

The Names are coming from the database, user will input the score on button click I want the linq code position result to display on the Position
column. Thanks.

What I have tried:

VB
' Example user scores
    Dim userScores = {
        New With {.Score = 65, .Name = "Linda"},
        New With {.Score = 65, .Name = "Paul"},
        New With {.Score = 64, .Name = "John"},
        New With {.Score = 63, .Name = "Yoko"}
    }
    ' Transform user scores to a high score table
    Dim table = _
        userScores _
        .OrderByDescending(Function(user) user.Score) _
        .Select(Function(user, i) New With {.Index = i + 1, .User = user}) _
        .GroupBy(Function(row) row.User.Score) _
        .SelectMany(
            Function(group)
                Dim position = group.First().Index
                Return group.Select(
                    Function(row)
                    Return New With {.Position = position, .User = row.User}
                End Function)
            End Function)
    ' Print results
    For Each row In table
        Console.WriteLine("{0} {1} {2}", row.Position, row.User.Score, row.User.Name)
    Next
Posted
Updated 18-Feb-16 9:35am
v3
Comments
Maciej Los 18-Feb-16 15:23pm    
Not sure what you want to achieve... Why some columns are bounded and other not? You should bind all columns...

1 solution

Please, read my comment to the question.

As per my understanding, you should bind all colums to result list, as MSDN states:
Quote:
The DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements one of the following interfaces:
  • The IList interface, including one-dimensional arrays.
  • The IListSource interface, such as the DataTable and DataSet classes.
  • The IBindingList interface, such as the BindingList(Of T) class.
  • The IBindingListView interface, such as the BindingSource class.


This is how you can achieve that:
VB
Dim table = (...).ToList()
DataGridView1.DataSource = table


See: DataGridView.DataSource Property (System.Windows.Forms)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900