从DGV获取唯一值 [英] Get Unique values from DGV
本文介绍了从DGV获取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在试图弄清楚如何从datagridview中的唯一值计算总数。
给你这个例子会更容易:
未结合的DGV1
客户ID值等等等
234 400.34
234 500.1
222 200.44
234 620.34
111 130.34
234 450.99
111 234.56
222 500.45
DGV2上的输出
客户ID值等等等等
111 364.9
222 700.89
234 1971.77
或类似/在同一DGV1上的输出
客户ID值等等等等等等
111 130.34
111 234.56
总计364.9
222 200.44
222 500.45
总计700.89
234 400.34
234 500.1
234 620.34
234 450.99
总计1971.77
如果这对我想要实现的目标有任何意义。
在此先感谢您的帮助
thx MJ
解决方案
假设DataGridView.DataSource [ ^ ]是 DataTable [ ^ ],您可以使用Linq按ClientID
对数据进行分组。
示例代码:
Dim dt As DataTable = 新 DataTable()
Dim dc As DataColumn = 新 DataColumn( 客户端ID,类型。 GetType ( System.Int32))
dt.Columns.Add(dc)
dc = 新 DataColumn( Value,Type。 GetType ( System.Double))
dt.Columns .Add(dc)
dt.Rows.Add( New Object (){ 234 , 400 。 34 })
dt.Rows.Add(新 对象(){ 234 , 500 。 1 })
dt.Rows.Add( New 对象(){ 222 , 200 。 44 })
dt.Rows.Add( New 对象(){ 234 , 620 。 34 })
dt.Rows.Add(新 对象(){ 111 , 130 。 34 })
dt.Rows.Add( New Object (){ 234 , 450 。 99 })
dt.Rows.Add( New Object (){ 111 , 234 。 56 })
dt.Rows.Add( New Object (){ 222 , 500 。 45 })
Dim groupedRows = dt.AsEnumerable()。GroupBy( Function (a)a.Field( 整数)( 客户端ID))。[选择](功能(g) 新 使用 {_
Key .ClientID = g.Key(),_
Key .Value = g.Sum( Functi on (a)a.Field( Of Double )( 值))_
})。ToList()
现在,您可以将此变量用作数据源:
DataGridView2.DataSource = groupedRows
groupedRows
返回类似的内容:
ClientID值
234 1971.77
222 700.89
111 364.9
有关详细信息,请参阅: LINQ - 示例查询 [ ^ ]
祝你好运!
工作样本 [ ^ ]
Hi guys
I am trying to figure out how to calculate totals from unique values in a datagridview.
It will be easier to give you this example:
Unbound DGV1 Client ID Value etc etc etc 234 400.34 234 500.1 222 200.44 234 620.34 111 130.34 234 450.99 111 234.56 222 500.45 Output like this on DGV2 Client ID Value etc etc etc 111 364.9 222 700.89 234 1971.77 or output like / on same DGV1 Client ID Value etc etc etc 111 130.34 111 234.56 Total 364.9 222 200.44 222 500.45 Total 700.89 234 400.34 234 500.1 234 620.34 234 450.99 Total 1971.77
if this makes any sense what i'm trying to achieve.
Thanks in advance for the help
thx MJ
解决方案
Assuming that DataGridView.DataSource[^] is DataTable[^], you can use Linq to group data byClientID
.
Sample code:
Dim dt As DataTable = New DataTable() Dim dc As DataColumn = New DataColumn("Client ID", Type.GetType("System.Int32")) dt.Columns.Add(dc) dc = New DataColumn("Value", Type.GetType("System.Double")) dt.Columns.Add(dc) dt.Rows.Add(New Object(){234, 400.34}) dt.Rows.Add(New Object(){234, 500.1}) dt.Rows.Add(New Object(){222, 200.44}) dt.Rows.Add(New Object(){234, 620.34}) dt.Rows.Add(New Object(){111, 130.34}) dt.Rows.Add(New Object(){234, 450.99}) dt.Rows.Add(New Object(){111, 234.56}) dt.Rows.Add(New Object(){222, 500.45}) Dim groupedRows = dt.AsEnumerable().GroupBy(Function(a) a.Field(Of Integer)("Client ID")).[Select](Function(g) New With { _ Key .ClientID = g.Key(), _ Key .Value = g.Sum(Function(a) a.Field(Of Double)("Value")) _ }).ToList()
Now, you can use this variable as an DataSource:
DataGridView2.DataSource = groupedRows
groupedRows
return something like:
ClientID Value 234 1971.77 222 700.89 111 364.9
For further information, please see: LINQ - Sample Queries[^]
Good luck!
[EDIT]
Working sample[^]
这篇关于从DGV获取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文