下午好。
我有一个问题,我不知道它已经完成或是新的。是否可以根据datagridview中的checked列多次重复相同的过程?
这是方案
在我有一个名为ID
,Name
和Cash
的3列的Datagridview之前,每次我点击一行,数据将在3个标签中传输,之后我会放文本框中的数字,用于在将数据保存到数据库之前计算现金。
现在这是我的目标,我将在checkboxcolumn
旁边的datagridview的开头添加ID
,这是下一部分让我说我将检查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未来的帮助
答案 0 :(得分:1)
仔细阅读我在此代码中放置的Comments
,以便您了解正在进行的操作。
我已经在这里添加了CellClick
的代码(我用CellValueChanged
替换了代码,以及用于保存仅检查行的代码。
您可以在SaveCheckedRecords()
。{/ p>之一中致电button_Clicks
我还添加了 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
如果有错误(因为我没有对代码进行测试)或您仍然不了解的内容,请在此处进行评论。
答案 1 :(得分:0)
尝试使用循环。
实施例
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
也许这就是它?试试先生