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

如何在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语句处理,不过,一并谢过了!我要多跟各位学习交流! __________________________________________________________________________

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...