Angustia Abutan Tulao Angustia Abutan Tulao - 4 years ago 103 Question

loop through datagrid values and use the value for other datagridview

I have a datagridview and i want to get values of the model column and get the count of the model for the other datagridview but i dont know how to loop through the value of the cells from the first datagrid.

This is the screenshot

What i want is to make the count datagrid on the right side to have 2 columns also. This is my code:

Private Sub jessy()
Dim count As Integer = 0
Dim a = requestDatagridview.Rows.Count
For Each row As DataGridViewRow In requestDatagridview.Rows
Label1.Text = row.Cells("model").Value
conn = New MySqlConnection
conn.ConnectionString = "server=; port=3306; username=root; password=""; database= atos_db"

Dim dbDataset1 As New DataTable
Dim sda1 As New MySqlDataAdapter
Dim bsource1 As New BindingSource
Dim query As String
query = "select count(serial_no) as 'Count' from atos_db.equip_tbl left join atos_db.itemdetails_tbl on equip_tbl.itemdetails_id=itemdetails_tbl.itemdetails_id left join atos_db.brand_tbl on itemdetails_tbl.brand_id = brand_tbl.brand_id left join atos_db.item_tbl on brand_tbl.item_id=item_tbl.item_id where model='" & a & "'"

comm = New MySqlCommand(query, conn)

sda1.SelectCommand = comm
bsource1.DataSource = dbDataset1
countDatagridview.DataSource = bsource1
Catch ex As MySqlException
End Try

End Sub

Answer Source

The following code may help you :

' Create a variable to store your value (example qty)
Dim total As Integer = 0

' Loop through your datagridview rows.
For i As Integer = 0 To dtgv.Rows.Count - 1 
' Add the qty value of the current row to total
total = total + dtgv.Rows(i).Cells(4).Value

' Then, modify the value of the second dtgv
dtgv2.Rows(0).Cells(0).Value = total
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download