gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:48:01 26 4
gpt4 key购买 nike

下午好。

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

场景是这样的

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

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

这是我目前尝试过的

这是在labels中传递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()

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

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

enter image description here

这是代码的输出

enter image description here

我希望我能解决我的问题。

future 帮助的 TYSM

最佳答案

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

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

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

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

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

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

关于mysql - 使用 Datagridview 中的复选框列重复一个过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com