更新时间:2023-11-30 21:37:34
在第二个for循环外移动创建和添加dt_C行Dim nrow As DataRow = dt_diff.NewRow
For i = 0 至dt_diff.Columns.Count - 1
val1 = dt_local.Rows( 0 )。项目(i)
val2 = dt_azure.Rows( 0 )。项目(i)
nrow(i)= val1 - val2
下一个
dt_diff.Rows.Add(nrow)
干杯
How to insert data into datatable from the question?
Visual Studio 2015 | Console Application
I have 3 datatable named dt_A, dt_B, dt_C. I have made queries to SQL for counting table row count and fill SQLDataReader into DataTable. Involved DataTable is dt_A (grab from serverA) and dt_B (grab from serverB). While dt_C will contain a total different count from (dt_A minus dt_B).
Assuming both dt_A and dt_B should have this column:
dt_A count_tbl1...........390 count_tbl2.............9 count_tbl3...........500
dt_B count_tbl1...........385 count_tbl2.............0 count_tbl3...........480
Assume dt_C will hold differentiation from above data
dt_C count_tbl1.............5 count_tbl2.............9 count_tbl3............20
dt_A and dt_B automatically fill on queries. dt_C will insert manually.
Dim val1, val2 As Integer
Dim colname As String
For i = 0 To dt_A.Columns.Count - 1
colname = dt_A.Columns(i).ToString
dt_C.Columns.Add(colname)
Next
For i = 0 To dt_C.Columns.Count - 1
Dim nrow As DataRow = dt_C.NewRow
val1 = dt_A.Rows(0).Item(i)
val2 = dt_B.Rows(0).Item(i)
nrow(i) = val1 - val2
dt_C.Rows.Add(nrow)
Next
The second For Next, is showing wrong display. It should be adding on single row. Instead, while lopping, it will add next value to next column but next row.
I want next value to be on next column and on row(0). No any other row should be made. I have try some other but shows empty
image view here[^]
What I have tried:
Dim val1, val2 As Integer
Dim colname As String
For i = 0 To dt_A.Columns.Count - 1
colname = dt_A.Columns(i).ToString
dt_C.Columns.Add(colname)
Next
For i = 0 To dt_C.Columns.Count - 1
Dim nrow As DataRow = dt_C.NewRow
val1 = dt_A.Rows(0).Item(i)
val2 = dt_B.Rows(0).Item(i)
nrow(i) = val1 - val2
dt_C.Rows.Add(nrow)
Next
Move the creation and adding of the dt_C row outside the second for-loopDim nrow As DataRow = dt_diff.NewRow For i = 0 To dt_diff.Columns.Count - 1 val1 = dt_local.Rows(0).Item(i) val2 = dt_azure.Rows(0).Item(i) nrow(i) = val1 - val2 Next dt_diff.Rows.Add(nrow)
Cheers