Vb.net使用linq来显示位置datagrid [英] Vb.net using linq to display position datagrid

查看:84
本文介绍了Vb.net使用linq来显示位置datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请先生,我如何在数据绑定DataGridView中实现下面的linq代码。我的DataGridView绑定到数据集,但我有一个未绑定的列,我想显示linq代码的输出位置。我的DataGridView看起来像这样



名称总分位置



名称来自数据库,用户将按钮点击输入分数我希望linq代码位置结果显示在位置

列上。谢谢。



我的尝试:



  用户得分示例 
Dim userScores = {
使用 {.Score = 65 ,。Name = Linda},
{.Score = 65 ,。Name = Paul},
{.Score = 64 ,。Name = John},
使用 {.Sco re = 63 ,。Name = Yoko}
}
' 将用户分数转换为高分表
Dim table = _
userScores _
.OrderByDescending( Function ( user)user.Score)_
选择功能(用户,i) {.Index = i + 1 , .User = user})_
.GroupBy( Function (row)row.User.Score)_
.SelectMany(
功能(组)
Dim position = group.First()。索引
返回组。选择
功能(行)
返回 使用 {.Position = position,.User = row.User}
结束 功能
< span class =code-keyword>结束 功能
' < span class =code-comment>打印结果
对于 每行表中
Console.WriteLine( { 0} {1} {2},row.Position,row.User.Score,row.User.Name)
下一步

解决方案

请阅读我对该问题的评论。



根据我的理解,您应该将所有列绑定到结果列表,如MSDN所述:

Quote:

DataGridView类支持标准的Windows Forms数据绑定模型。这意味着数据源可以是实现以下接口之一的任何类型:

  • IList接口,包括一维数组。
  • IListSource接口,例如DataTable和DataSet类。
  • IBindingList接口,例如BindingList(Of T)类。
  • IBindingListView接口,例如BindingSource类。 / li>





这是你如何做到这一点:

  Dim  table =(...)。ToList()
DataGridView1.DataSource = table





请参阅: DataGridView.DataSource属性(System.Windows.Forms) [ ^ ]


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:

' 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

解决方案

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:

Dim table = (...).ToList()
DataGridView1.DataSource = table



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


这篇关于Vb.net使用linq来显示位置datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆