如何计算vb.net 2010中同一datagridview的另一个单元格中datagridview的单元格值 [英] how to calculate cells value of datagridview in another cell of same datagridview in vb.net 2010

查看:100
本文介绍了如何计算vb.net 2010中同一datagridview的另一个单元格中datagridview的单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据数据网格视图中的数量和价格计算总金额和总税额,方法是选择商品代码,然后在数据网格的数量单元格中输入数量,我在下面的代码中显示项目名称和价格,但无法计算总价格和总税金



请帮帮我?



i有表名项目

 itemcode itemname数量价格税总额总税金
1 led tv 2 12000 12%



 < span class =code-keyword>私有  Sub  DataGridView1_CellValueChanged( ByVal  sender 作为 对象 ByVal  e  As  System.Windows.Forms.DataGridViewCellEventArgs)句柄 DataGridView1.CellValueChanged 
如果 flag = False 然后
getneame(e.RowIndex,e.ColumnIndex,DataGridView1)
结束 如果
结束 Sub

私有 Sub getneame(< span class =code-keyword> ByVal intRow 作为 整数 ByVal intCol 作为 整数 ByRef dgvDetails As DataGridView)

试用
如果 intCol = 1 那么
' ProductID组合框更改
Dim itemcod As < span class =code-keyword> String = CStr (dgvDetails.Rows(intRow).Cells( 1 )。价值)

Dim decPrice As 十进制
Dim intRowCtr As 整数
Dim rowProd As DataRow
Dim strName As String = Nothing

' 搜索数据ProductID表和更新UnitPrice
使用 ds.Tables( 0
对于 intRowCtr = 0 .Rows.Count - 1
rowProd = .Rows(intRowCtr)
If CStr (rowProd.Item( 0 ))= itemcod 然后
a = CStr (rowProd.Item( 1 ))
b = CDec (rowProd.Item( 4 ))
decPrice = CDec (rowProd.Item( 3 ))

Dim x,y As Decimal
x = CDec (rowProd.Item( 5 ))
Dim z 作为 整数 = CInt (dgvDetails1 .Rows(intRow1).Cells( 3 )。值)
decPrice = CDec (rowProd .Item( 3 ))

y = x * decPrice * z

' 更改UnitPrice值
使用 DataGridView1
。行(介绍w)。细胞( 2 )。值= a
。行(intRow)。细胞( 4 )。Value = decPrice
.Rows(intRow1).Cells( 5 )。Value = y
退出 对于
结束 使用
结束 如果
下一步 intRowCtr
结束 使用
< span class =code-keyword>结束 如果
Catch exc < span class =code-keyword> As 异常
' MsgBox(exc.Message + exc.StackTrace,,exc.Source)
结束 尝试

结束 Sub

解决方案

假设 DataGridView.DataSource [ ^ ]是 DataTable [ ^ ],我建议计算 total amt 使用 Linq To Dataset [ ^ ]:



 昏暗 dt  As  DataTable = DataGridView1.DataSource 
Dim id = dgvDetails.Rows(e。 RowIndex)。细胞( 1 )。值
Dim totaltax = dt.AsEnumerable()_
.Where( Function (item)item.Field( of 整数)( itemcode)= id)_
选择功能(item)item.Field( 整数)( qty)* _
item.Field( Of Decimal )( price)* _
item.Field( Of 十进制)( tax ))。SingleOrDefault()

Dim totalamt = dt.AsEnumerable()_
.Where( Function (item)item.Field( Of Integer )( itemcode)= id)_
选择 Function (item)item.Field( Of 十进制)( price)+ totalt ax).SingleOrDefault()

使用 DataGridView1
.Rows(e.RowIndex).Cells( 6 )。Value = totalamt
.Rows(e.RowIndex).Cells( 7 )。Value = totaltax
结束





如需了解更多信息,请参阅:

LINQ to DataSet示例 [ ^ ]

查询数据集(LINQ to DataSet) [ ^ ]

LIN中的查询Q to DataSet [ ^ ]



完整示例:

  Dim  dt  As  DataTable =  DataTable()
dt .Columns.Add( New DataColumn( itemcode GetType 整数)))
dt.Columns.Add( DataColumn( itemname GetType String )))
dt.Columns.Add( DataColumn( qty的GetType (<温泉n class =code-keyword> Integer )))
dt.Columns.Add( New DataColumn( price GetType 十进制)))
dt.Columns.Add( DataColumn( tax GetType 十进制 )))
dt.Columns.Add( New DataColumn( total amt GetType Decimal )))
dt.Columns.Add( New DataColumn( total tax GetType Decimal )))

dt .Rows.Add(<跨度class =code-keyword>新 对象(){ 1 led tv 2 12000 0 12 ,DBNull.Value,DBNull。值})

Dim id = 1
Dim totaltax = dt.AsEnumerable()_
.Where( Function (item)item.Field(< span class =code-keyword> 整数)( itemcode)= id)_
选择 Function (item)item.Field( Of Integer )( <跨度class =code-string> qty)* _
item.Field( of 十进制)( price)* _
item.Field(< span class =code-keyword> 十进制)( tax))。SingleOrDefault()

Dim totalamt = dt.AsEnumerable()_
。其中(功能(item)item.Field( of Integer )( itemcode)= id)_
选择功能(item)item.Field( Of 十进制)( 价格)+ totaltax).SingleOrDefault()

Console.WriteLine( tax:{0}总计:{1},totaltax,totalamt)



退货:

< pre lang =text>税:2880.00总计:14880.00


i want to calculate total amt and total tax according qty and price in datagrid view by selecting item code and entering qty in qty cell of datagrid i used below code it shows item name and price but could not calculate total price and total tax

please help me?

i have table name item

itemcode itemname qty price   tax   total amt    total tax
1         led tv   2   12000  12%


Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    If flag = False Then
        getneame(e.RowIndex, e.ColumnIndex, DataGridView1)
    End If   
End Sub

Private Sub getneame(ByVal intRow As Integer, ByVal intCol As Integer, ByRef dgvDetails As DataGridView)

    Try
        If intCol = 1 Then
            'ProductID combo box change
            Dim itemcod As String = CStr(dgvDetails.Rows(intRow).Cells(1).Value)

            Dim decPrice As Decimal
            Dim intRowCtr As Integer
            Dim rowProd As DataRow
            Dim strName As String = Nothing
           
            'Search the DataTable for the ProductID and update UnitPrice
            With ds.Tables(0)
                For intRowCtr = 0 To .Rows.Count - 1
                    rowProd = .Rows(intRowCtr)
                    If CStr(rowProd.Item(0)) = itemcod Then
                        a = CStr(rowProd.Item(1))
                        b = CDec(rowProd.Item(4))
                        decPrice = CDec(rowProd.Item(3))

Dim x, y As Decimal
                        x = CDec(rowProd.Item(5))
                        Dim z As Integer = CInt(dgvDetails1.Rows(intRow1).Cells(3).Value)
                        decPrice = CDec(rowProd.Item(3))

                        y = x * decPrice * z
                                                                                  
                        'Change UnitPrice value
                        With DataGridView1
                            .Rows(intRow).Cells(2).Value = a
                            .Rows(intRow).Cells(4).Value = decPrice
                            .Rows(intRow1).Cells(5).Value = y
                            Exit For
                        End With
                    End If
                Next intRowCtr
            End With
        End If
    Catch exc As Exception
        'MsgBox(exc.Message + exc.StackTrace, , exc.Source)
    End Try

End Sub

解决方案

Assuming that DataGridView.DataSource[^] is DataTable[^], i'd suggest to calculate tax and total amt using Linq To Dataset[^]:

Dim dt As DataTable = DataGridView1.DataSource
Dim id = dgvDetails.Rows(e.RowIndex).Cells(1).Value
Dim totaltax = dt.AsEnumerable() _
        .Where(Function(item) item.Field(Of Integer)("itemcode")=id) _
        .Select(Function(item) item.Field(Of Integer)("qty") * _
            item.Field(Of Decimal)("price") * _
            item.Field(Of Decimal)("tax")).SingleOrDefault()

Dim totalamt = dt.AsEnumerable() _
        .Where(Function(item) item.Field(Of Integer)("itemcode")=id) _
        .Select(Function(item) item.Field(Of Decimal)("price") + totaltax).SingleOrDefault()

With DataGridView1
    .Rows(e.RowIndex).Cells(6).Value = totalamt
    .Rows(e.RowIndex).Cells(7).Value = totaltax
End With



For further information, please see:
LINQ to DataSet Examples[^]
Querying DataSets (LINQ to DataSet)[^]
Queries in LINQ to DataSet[^]

Complete example:

Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("itemcode", GetType(Integer)))
dt.Columns.Add(New DataColumn("itemname", GetType(String)))
dt.Columns.Add(New DataColumn("qty", GetType(Integer)))
dt.Columns.Add(New DataColumn("price", GetType(Decimal)))
dt.Columns.Add(New DataColumn("tax", GetType(Decimal)))
dt.Columns.Add(New DataColumn("total amt", GetType(Decimal)))
dt.Columns.Add(New DataColumn("total tax", GetType(Decimal)))

dt.Rows.Add(New Object(){1, "led tv", 2, 12000, 0.12, DBNull.Value, DBNull.Value})

Dim id = 1
Dim totaltax = dt.AsEnumerable() _
        .Where(Function(item) item.Field(Of Integer)("itemcode")=id) _
        .Select(Function(item) item.Field(Of Integer)("qty") * _
            item.Field(Of Decimal)("price") * _
            item.Field(Of Decimal)("tax")).SingleOrDefault()

Dim totalamt = dt.AsEnumerable() _
        .Where(Function(item) item.Field(Of Integer)("itemcode")=id) _
        .Select(Function(item) item.Field(Of Decimal)("price") + totaltax).SingleOrDefault()

Console.WriteLine("tax: {0} total: {1}", totaltax, totalamt)


Returns:

tax: 2880.00 total: 14880.00


这篇关于如何计算vb.net 2010中同一datagridview的另一个单元格中datagridview的单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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