繁体   English   中英

使用 Datagridview 中的复选框列重复一个过程

[英]Repeat a Procedure using Checkbox column in Datagridview

下午好。

我有一个问题,我不知道它是已经完成的还是新的。 是否可以根据 datagridview 中的选中列多次重复相同的过程?

这是场景

在我有一个 Datagridview 之前,它有一个名为IDNameCash的 3 列,每次我单击一行时,数据将在 3 个标签中传输,之后我将在文本框中输入一个数字,该数字将在将现金保存到数据库之前计算现金。

现在这是我的目标,我将在ID旁边的 datagridview 的开头添加一个checkboxcolumn ,这是下一部分 假设我将检查 2 行。 有没有可能他们两个会走同一个程序? (在一一保存到数据库之前转移到标签计算?)

这是我到目前为止尝试过的

这是标签中datagridview数据传输的代码(在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()

这是计算部分(在私有子标准下)

 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

这是保存部分或传输到数据库(单击按钮)

  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()

在此处输入图片说明

这是代码的输出

在此处输入图片说明

我希望我清楚我的问题。

TYSM 未来的帮助

仔细阅读我在此代码中放置的Comments ,以便您了解发生了什么。

我已经在此处包含了CellClick的代码(我将其替换为CellValueChanged保存选中行的代码。

您可以在button_Clicks之一中调用SaveCheckedRecords()

我还包含了一个用于计算Unit Price总价的Bonus代码。

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

如果有错误(因为我没有测试过代码)或者你仍然不明白的地方,只需在这里评论。

尝试使用循环。

例子

for each row as datagridviewrow in dg.rows
 if row.cell(NameOfChckbox).value = 'True' then
  'Transfer dg data to to label
  'insert your computation/call it
  'save to database
 end if
next

希望这是你想要的。 如果我的答案中缺少某些内容或不清楚,请通知我:)

- - - 建议 - - -

我的回答中有一些建议。 这不适用于 cellclick。 因为用户有时会犯错误,例如错误点击或点击前想太多所以我建议在点击 dg 中的多个复选框后使用一个按钮来调用这个函数。

- - - 最新更新 - - -

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 for each row as datagridviewrow in dg.rows
  if row.cell("NameOfChckboxCell").value = "True" then
   label1.text = row.cells("ItemCode").value
   etc . . .
   then call your standard sub?
   standard()
   then your save here. . .
  end if
End Sub

也许就是这样? 试试吧先生

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM