want to search items using datagridview 1 as my search Engine and search to datagridview2 and display the result to datagridview2
it must go like this
want to search items using datagridview 1 as my search Engine and search to datagridview2 and display the result to datagridview2
it must go like this
Assuming you have a DataTable
defined (you should, if you want to work with grid data), you can define and use following method:
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;
}
To get sum of som column for particular search string:
var colSum = SumValues(dt1, 5, "Ref 5");
To place the sum into the 2nd table:
DataTable2("SumOfValues")(5) = colSum;
You can work with DataGridView
values directly:
DataGridView1.Rows(iRow).Cells("SumOfValues").Value = ColSum;
...but I wouldn't recommend it. You can get into trouble with DataTypes and more.
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