vb.net中如何利用DataTable中的資料進行計算

例如
If Cell(SimplyCell(i).NodeIndex).Type = "zsz" Then
ZszXH = GetZszIndex(Cell(SimplyCell(i).NodeIndex).Name)
H(i) = Zsz(ZszXH).ActGY * 102
C(i) = Zsz(ZszXH).ActQ '//-SimplyCell[i].SimplyNodeQ; '//-Zsz[ZszXH].ActZc;
代碼中的 ActGY 就是我表格中的“實際站壓(MPa)”
ActQ 就是我表格中的“實際站排量(m^3/d)”
計算的代碼是寫在了一個模塊里
創建DataTable的代碼是寫在視窗類里
是否可以在計算模塊中呼叫視窗類里面的資料?
謝謝各位啦!!!
uj5u.com熱心網友回復:
有時候,難以解決的不是問題,而是讓人看不懂的題目表達方式....想用模塊的計算方法來計算基面的值,將dataTable以byRef的方式傳給計算代碼即可!算完了,寫入datatable,界面會自動變化,如果么有,重繪下也有了。
uj5u.com熱心網友回復:
把數值以引數的形式傳遞進去不就可以了?uj5u.com熱心網友回復:
你的datatable有沒有系結資料源,如果系結了,你要更新資料源,否則一重繪它又回去了。是否可以在計算模塊中呼叫視窗類里面的資料? 可以的,你把該控制元件的modifer屬性改為Public就可以了, 比如Form1.DataTable1
uj5u.com熱心網友回復:
我是這樣寫的Private Sub cmdDBInput_Click(sender As Object, e As EventArgs) Handles cmdDBInput.Click
、、、、、、
For i = 0 To ZszNum - 1
For j = 0 To topics.Tables(0).Rows.Count - 1
If t = topics.Tables(0).Rows(j).Item("RQ") Then
If topics.Tables(0).Rows(j).Item("ZM") = aa.Rows(i)(0) Then
aa.Rows(i)(1) = topics.Tables(0).Rows(j).Item("RCKSL")
aa.Rows(i)(2) = topics.Tables(0).Rows(j).Item("GY")
End If
End If
Next
Next
其中topics是我的資料源表格,aa是我提取所需資料建立的DataTable
這里的aa.Rows(i)(1)是我計算中的Zsz(i).ActQ
aa.Rows(i)(2)是我計算中的Zsz(i).ActGY
怎么以引數的形式傳出去
uj5u.com熱心網友回復:
用DataColumn.Expression來實作.就是聚合列,跟Excel的公式用法一樣.
Private Sub CalcColumns()
Dim rate As Single = .0862
Dim table As New DataTable()
' Create the first column.
Dim priceColumn As New DataColumn()
With priceColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "price"
.DefaultValue = 50
End With
' Create the second, calculated, column.
Dim taxColumn As New DataColumn()
With taxColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "tax"
.Expression = "price * 0.0862"
End With
' Create third column
Dim totalColumn As New DataColumn()
With totalColumn
.DataType = System.Type.GetType("System.Decimal")
.ColumnName = "total"
.Expression = "price + tax"
End With
' Add columns to DataTable
With table.Columns
.Add(priceColumn)
.Add(taxColumn)
.Add(totalColumn)
End With
Dim row As DataRow= table.NewRow
table.Rows.Add(row)
Dim view As New DataView
view.Table = table
DataGrid1.DataSource = view
End Sub
DataColumn.Expression 屬性 (System.Data) | Microsoft Docs
https://docs.microsoft.com/zh-cn/dotnet/api/system.data.datacolumn.expression?view=netframework-4.8
uj5u.com熱心網友回復:
首先包含datatable的表單打開時不能new,而是直接呼叫表單名稱。form1.show
在表單代碼里定義一個公共類資料表,用來存盤你的資料。其他模塊計算需要呼叫這個表單的資料表就直接呼叫行了。
uj5u.com熱心網友回復:
寫個foreach回圈把datatable讀一下,在回圈中取值并計算,效率比較高,要么建議采用weixin_45823259所說有方案.轉載請註明出處,本文鏈接:https://www.uj5u.com/net/66438.html
標籤:VB.NET
