[英]I want add data from datagridview to another form - not display data
[英]I Want to search Specific item from another datagridview to another datagridview and get the sum of the value
假設您定義了一個DataTable
(如果您想使用網格數據,您應該),您可以定義和使用以下方法:
C#:
public void SumValues(ref DataTable DataTable1, Int32 ColIdx, string SearchStr)
{
Int32 RetSum; // change datatype, if needed
foreach (DataRow row in DataTable1.Rows)
{
if (!IsDBNull(row(0)) && !IsNothing(row(ColIdx)) && row(ColIdx) == SearchStr && !IsDBNull(row(ColIdx)) && !IsNothing(row(ColIdx)))
RetSum += row(ColIdx);// add a value from column with index ColIdx, if value of the first column is SearchStr
}
return RetSum;
}
要獲取特定搜索字符串的 som 列的總和:
var colSum = SumValues(dt1, 5, "Ref 5");
要將總和放入第二個表中:
DataTable2("SumOfValues")(5) = colSum;
您可以直接使用DataGridView
值:
DataGridView1.Rows(iRow).Cells("SumOfValues").Value = ColSum;
......但我不會推薦它。 您可能會遇到數據類型等問題。
VB.NET:
Public Function SumValues(ByRef DataTable1 As DataTable, ColIdx As Int32, SearchStr As String)
Dim RetSum As Int32 ' change datatype, if needed
For Each row As DataRow In DataTable1.Rows
If Not IsDBNull(row(0)) AndAlso Not IsNothing(row(ColIdx)) AndAlso row(ColIdx) = SearchStr AndAlso Not IsDBNull(row(ColIdx)) AndAlso Not IsNothing(row(ColIdx)) Then
RetSum += row(ColIdx) ' add a value from column with index ColIdx, if value of the first column is SearchStr
End If
Next
Return RetSum
End Function
Dim colSum = SumValues(dt1, 5, "Ref 5")
DataTable2("SumOfValues")(5) = colSum
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.