微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在vb.net中按dataview分组

如何解决如何在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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?