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

VB.NET源码-156个实用实例哦…… <script type="text/javascript"> </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语句处理,不过,一并谢过了!我要多跟各位学习交流!
__________________________________________________________________________
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一新追加行非表示 4. DataGridView判断当前选行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView不表示和删除 DataGridView控件用法合集(二) 7. DataGridView宽度高度设置为不能编辑 8. DataGridView行高幅自动调整 9. DataGridView指定行冻结 10. DataGridView顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView显示选择框CheckBox 46. DataGridView显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView显示按钮 49. DataGridView显示链接 50. DataGridView显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一显示相应图片 57. DataGridView显示进度条(ProgressBar) 58. DataGridView添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridViewEnter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值