如何解决如何在vb.net中按dataview分组
我想在数据视图上应用分组依据并应用过滤器。
SELECT xAccID,xAccName,xProdID,xProdName,xPrice,SUM(xQuan) AS xxQuan,xDate,SUM(xCarCount) AS xxCarCount FROM
(
(SELECT
`FatoraDate` AS xDate,`FatoraReso` AS xAccID,`AccName` AS xAccName,`FatoraProduct` AS xProdID,CONCAT(`CategoryName`,' ',`ProductName`) AS xProdName,`FatoraPurPrice` AS xPrice,SUM(`FatoraQuan`) AS xQuan,COUNT(`FatoraReso`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraReso
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xPrice
ORDER BY xDate,xAccID,xProdID)
UNION ALL
(SELECT
`FatoraDate` AS xDate,`FatoraCustomer` AS xAccID,`FatoraSalePrice` AS xPrice,COUNT(`FatoraCustomer`) AS xCarCount
FROM tblfatora
INNER JOIN tblaccounts
ON tblaccounts.AccID = tblfatora.FatoraCustomer
INNER JOIN tblproducts
ON tblproducts.ProductID = tblfatora.FatoraProduct
INNER JOIN tblcategories
ON tblcategories.CategoryID = tblproducts.ProductCategory
GROUP BY xAccID,xProdID
)) tbl1
GROUP BY xDate,xPrice
ORDER BY `tbl1`.`xAccName` ASC
我在加载时填写了MyVar_Dt_Quan,并预过滤了过滤器:
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.defaultview
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xProdID = row.Field(Of Double)("xProdID"),Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(grp.Key.xProdID,grp.Key.xPrice,grp.Sum(Function(row) row.Field(Of Double)("xxQuan")))
Next
Me.Dgv2.DataSource = tableResult
我想对xProdID和xPrice进行分组,并且过滤器为(xAccID =?和(xDate = ??)),并获得xQuan和xCarCount的总和进行分组。 我该如何应用?
解决方法
MyPubVar_Sort = "xProdID,xPrice"
MyPubVar_Filter = ""
MyPubVar_Filter = " xAccID = " & xAccID & "
AND
(xDate >= '" & xDate1 & "' AND xDate <= '" & xDate2 & "') "
MyVar_Dv_Quan = MyVar_Dt_Quan.DefaultView
MyVar_Dv_Quan.RowFilter = MyPubVar_Filter
Dim fruitGroups = MyVar_Dv_Quan.ToTable.AsEnumerable().
GroupBy(Function(row) New With {
Key .xAccID = row.Field(Of Int32)("xAccID"),Key .xAccName = row.Field(Of String)("xAccName"),Key .xProdID = row.Field(Of Int32)("xProdID"),Key .xProdName = row.Field(Of String)("xProdName"),Key .xPrice = row.Field(Of Double)("xPrice")
})
Dim tableResult = MyVar_Dv_Quan.ToTable.Clone()
For Each grp In fruitGroups
tableResult.Rows.Add(
grp.Key.xAccID,grp.Key.xAccName,grp.Key.xProdID,grp.Key.xProdName,grp.Key.xPrice,grp.Sum(Function(row) row.Field(Of Double)("xxQuan")),grp.Max(Function(row) row.Field(Of Date)("xDate")),grp.Sum(Function(row) row.Field(Of Object)("xxCarCount")))
Next
Me.Dgv2.DataSource = tableResult
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。