Paul Edward Pagente Paul Edward Pagente - 2 months ago 22
Vb.net Question

Repeat a Procedure using Checkbox column in Datagridview

Good Afternoon.

I have a question and I don't know if its already done or new. Is it possible to repeat a same procedure many times based on checked column in datagridview?

Here is the scenario

Before I have a Datagridview that has a 3 Columns named

ID
,
Name
and
Cash
everytime I will click a row the data will transfer in 3 Labels and after that I will put a number in textbox that will compute the cash before saving it in database.

Now here is my target I will add a
checkboxcolumn
on the start of datagridview next to
ID
and here is the next part Lets say i will check 2 rows. Is it possible that both of them will go in the same procedure? (Transfer to Labels the compute before saving to database one by one?)

This is what I tried so far

This is the code of transfering datagridview data in labels (under cellclick)

Dim i As Integer
i = DataGridView1.CurrentRow.Index
Label2.Text = DataGridView1.Item("ItemCode", i).Value
Label3.Text = DataGridView1.Item("Description", i).Value
Label4.Text = DataGridView1.Item("ReflectedQty", i).Value
Label5.Text = DataGridView1.Item("UOM", i).Value
Label6.Text = DataGridView1.Item("UnitPrice", i).Value
Label7.Text = DataGridView1.Item("Total", i).Value
Label8.Text = DataGridView1.Item("Remarks", i).Value
Dim cell As DataGridViewCheckBoxCell = DataGridView1.Rows(e.RowIndex).Cells(0)
DataGridViewCheckBoxColumn_Uncheck()
cell.Value = True
standard()


Here is the computation part (under private sub standard)

Dim con As MySqlConnection = New MySqlConnection("server=192.168.2.87;userid=root;password=****;database=inventory")
Dim cmd As MySqlCommand = New MySqlCommand("select StandardUOM,QtyPerUoM from item_master_list where ItemCode = '" & Label2.Text & "'", con)
Dim reader As MySqlDataReader
con.Open()
reader = cmd.ExecuteReader
While reader.Read
Label9.Text = reader.GetString("StandardUOM")
Label10.Text = reader.GetString("QtyPerUoM")
End While


Here is the saving part or transferring to database (button click)

DataGridView1.Columns.RemoveAt(0)
Dim con1 As MySqlConnection = New MySqlConnection("datasource=192.168.2.87;database=inventory;userid=root;password=*****")
Dim cmdinsert As MySqlCommand = New MySqlCommand("insert into receiving (RINo,PONo,ItemCode,Description,QtyPack,PackUoM,UnitPrice,Total,Remarks,ExpiryDate,QtyStan,StanUoM,PCS) values ('" & frm_Add_Receiving_Items.TextBox1.Text & "','" & Label1.Text & "','" & Label2.Text & "','" & Label3.Text & "','" & Label11.Text & "','" & Label5.Text & "','" & Label6.Text & "','" & Label7.Text & "','" & Label8.Text & "','" & DateTime.Now.ToString("yyyy-MM-dd") & "','" & Label12.Text & "','" & Label9.Text & "','" & Label10.Text & "')", con1)
con1.Open()
cmdinsert.ExecuteNonQuery()
con1.Close()


enter image description here

Here is the output of the code

enter image description here

I hope I clear my question.

TYSM for future help

Answer

Read the Comments I placed in this code carefully so you'll understand what's going on.

I already included here the code for CellClick (which I replaced with CellValueChanged and the code for saving only the checked rows.

You can call SaveCheckedRecords() in one of your button_Clicks.

I also included a Bonus code that compute for Unit Price 's total .

Sub DataGridView1_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles DataGridView1.CurrentCellDirtyStateChanged
    If DataGridView1.IsCurrentCellDirty Then
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellValueChanged
    If e.ColumnIndex = 0 Then 'SO THAT CHECKBOX COLUMN WILL ONLY TRIGGER THE CHANGES

        'THIS WILL HOLD THE VALUE OF THE CHECKBOX (TRUE OR FALSE)
        Dim currCheckCell As DataGridViewCheckBoxCell = _
          CType(DataGridView1.Rows(e.RowIndex).Cells(0), DataGridViewCheckBoxCell)

        'LABEL CHANGES BASED ON THE ROW OF THE CHECKBOX
        'IF-CONDITION SO THAT LABEL CHANGES WILL HAPPEN ONLY IF THE CHECKBOX IS CHECKED
        If currCheckCell.Value = True Then
            Dim i As Integer = e.RowIndex
            Label2.Text = DataGridView1.Item("ItemCode", i).Value
            Label3.Text = DataGridView1.Item("Description", i).Value
            Label4.Text = DataGridView1.Item("ReflectedQty", i).Value
            Label5.Text = DataGridView1.Item("UOM", i).Value
            Label6.Text = DataGridView1.Item("UnitPrice", i).Value
            Label7.Text = DataGridView1.Item("Total", i).Value
            Label8.Text = DataGridView1.Item("Remarks", i).Value
        End If

        Standard()

        Dim totalstr As Double = 0
        For Each drow As DataGridViewRow In DataGridView1.Rows
            Dim checkCell As DataGridViewCheckBoxCell = _
           CType(drow.Cells(0), DataGridViewCheckBoxCell)
            If checkCell.Value = True Then
                totalstr += Val(drow.Cells(5).Value)
            End If
        Next
        lblTotal.Text = FormatNumber(totalstr, 2)
    End If
End Sub


Public Sub SaveCheckedRecords()
    DataGridView1.Columns.RemoveAt(0)
    Dim con1 As MySqlConnection = New MySqlConnection("datasource=192.168.2.87;database=inventory;userid=root;password=*****")
    Dim cmdinsert As MySqlCommand = New SqlCommand
    For Each drow As DataGridViewRow In DataGridView1.Rows
        Dim checkCell As DataGridViewCheckBoxCell = _
       CType(drow.Cells(0), DataGridViewCheckBoxCell)
        If checkCell.Value = True Then 'AGAIN, TO CHECK IF THE COLUMN IS CHECKED
            'CELL INDEXES ARE ASSUMED SINCE YOU DIDN'T SPECIFIED IT ALSO
            'YOU ARE THE ONE WHO KNOWS THE RIGHT INDECES SO CHANGE THEM IF THE INDECES ARE WRONG
            Dim ItemCode As String = drow.Cells(1).Value
            Dim Desc As String = drow.Cells(2).Value
            Dim ReflectedQty As String = drow.Cells(3).Value
            Dim UOM As String = drow.Cells(4).Value
            Dim UnitPrice As Double = Val(drow.Cells(5).Value)
            Dim Total As Double = Val(drow.Cells(6).Value)
            Dim Remarks As String = drow.Cells(7).Value
            'NOW USE ALL OF THE VARIABLES ABOVE IN YOUR INSERT QUERY

            'CMDINSERT PART HERE SINCE I DON'T KNOW SOME OF YOUR LABELS 
            'LIKE LABEL11 SO IT'S UP TO YOU TO CREATE THE COMMAND WHICH IS EASY
            con1.Open()
            cmdinsert.ExecuteNonQuery()
            con1.Close()
        End If
    Next
End Sub

If there are errors (as I haven't tested the code) or things that you still don't understand, just comment it here .