[VB.NET]如何在Datagridview中进行列间的计算?

来源:互联网 发布:linux 查看网络端口 编辑:程序博客网 时间:2024/06/05 11:54
VB.NET源码-156个实用实例哦……<script type="text/javascript"><!--google_ad_client = "pub-8333940862668978";/* 728x90, 创建于 08-11-30 */google_ad_slot = "4485230109";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
如何在Datagridview中进行列间的计算?
举例:
Datagridview有三列,分别是price、quaty、sum
我想设置成sum=price*quaty
在论坛搜了一下贴,发现类似的问题不少,但我还是没有解决,请知道的兄弟指教,多谢
__________________________________________________________________________
具体困难在哪儿?
__________________________________________________________________________
我是由VFP刚转VB.NET的,在VFP中处理很简单:
replace sum with price*quaty
但在VB.net中,我不知道如何写这个代码
看过论坛里的一些贴子,有兄弟说在后台用SQL语句搞掂,但问题是我的datagridview的数据源是一个临时的Datatable,和后台没关啊。
楼上的兄弟,能否指教一二,感激啊!
__________________________________________________________________________
指教不敢当,呵呵。其实很简单,就是把前两项的数据相乘,放到第三项中。您只要会操作单元格中的数据自然就会了。写的潦草,还望见谅。

Public Class Form1

Dim conn As Data.OleDb.OleDbConnection
Dim da As Data.OleDb.OleDbDataAdapter
Dim ds As Data.DataSet

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DataGridView1.AllowUserToOrderColumns = False
conn = New OleDb.OleDbConnection( Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:/data.mdb ) 假设表中有两项: price 和 quaty
conn.Open()
da = New OleDb.OleDbDataAdapter( SELECT * FROM 表1 , conn)
ds = New Data.DataSet
da.Fill(ds)
conn.Close()
DataGridView1.DataSource = ds.Tables(0)
DataGridView1.Columns.Add( sum , sum ) 加入一项 sum
For i As Int32 = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(0).Value * DataGridView1.Rows(i).Cells(1).Value 把前两项的数据相乘,放到第三项中
Next
End Sub

End Class
__________________________________________________________________________
可以在sql语句里相办法
select price、quaty、price*quaty as sum from ....
__________________________________________________________________________
在DataGridView的CellEndEdit事件中加入以下代码即可:

Private Sub dgrd4_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgrd4.CellEndEdit
Try

If Me.dgrd4.Rows(e.RowIndex).IsNewRow = False Then
If IsNumeric(Me.dgrd4.Rows(e.RowIndex).Cells( Price ).Value) = False Then
Me.dgrd4.Rows(e.RowIndex).Cells( Price ).Value = Math.Round(0, 2)
End If

If IsNumeric(Me.dgrd4.Rows(e.RowIndex).Cells( quaty ).Value) = False Then
Me.dgrd4.Rows(e.RowIndex).Cells( quaty ).Value = Math.Round(0, 2)
End If


Me.dgrd4.Rows(e.RowIndex).Cells( sum ).Value = Math.Round(Me.dgrd4.Rows(e.RowIndex).Cells( Price ).Value * Me.dgrd4.Rows(e.RowIndex).Cells( quaty ).Value, 2)
End If
End If

End If

Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
End Try
__________________________________________________________________________
谢谢magicbacon(Cannot help coding)!
我要的就是:
For i As Int32 = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(2).Value = DataGridView1.Rows(i).Cells(0).Value * DataGridView1.Rows(i).Cells(1).Value 把前两项的数据相乘,放到第三项中
Next
感谢!散分
__________________________________________________________________________
也谢谢天地男儿!也是一正确方法!倦怠兄弟的办法我暂时没法用,S因为我在帖子里说了我没法到后台用SQL语句处理,不过,一并谢过了!我要多跟各位学习交流!
__________________________________________________________________________