[英]DataGridView : Update SQL Database when cell value changed
I have created a form which contains DataGridView and have created first column as checkbox column and fullrowselect is True. 我创建了一个包含DataGridView的表单,并创建了第一列作为复选框列,并且fullrowselect为True。
I want like when I click the checkbox of the row or change the checkbox value of the selected row, that particular record should be updated in the database... 我希望当我单击行的复选框或更改所选行的复选框值时,应该在数据库中更新该特定记录...
I wrote the query properly but facing some problems mentioned below. 我正确地编写了查询但遇到了下面提到的一些问题。
Now I first tried in cell click event... 现在我首先尝试了单元格点击事件...
Private Sub DGVHelp_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVHelp.CellClick
Try
If PubHelpCode = "ACTMAST" And e.ColumnIndex = 0 Then 'if only checkbox column is clicked
cmd = New SqlCommand("Update FAMPAR SET Tag = '" & DGVHelp(0, e.RowIndex).Value & "' where Account_Code = '" & DGVHelp(1, e.RowIndex).Value & "' ", con1)
ExecuteQuery(con1, "EDITCHECKBOX") ' Its my UDF
DGV_Reset()' This clears the datagrid and fetch the data again
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
When I click the checkbox cell (First column), the event first runs and then the value of the cell is changed... ie during cellclick event, the DGVHelp(0, e.RowIndex).Value doesn't change its value if its ticked or unticked. 当我单击复选框单元格(第一列)时,事件首先运行,然后单元格的值发生更改...即在单元格单击事件期间,DGVHelp(0,e.RowIndex).Value不会更改其值它的标记或未标记。
I even tried cellvaluechanged event but it hanged the program by some continuous process that I don't know... 我甚至尝试过cellvaluechanged事件,但它通过一些我不知道的连续过程挂起了程序...
Which event should I use to complete my task?? 我应该用哪个事件来完成我的任务?
If any other opinion/idea relating to this task is there then please share... 如果有任何其他与此任务相关的意见/想法,请分享...
The Code OneDrive link shared below.. 下面分享了Code OneDrive链接..
Solution Code link 解决方案代码链接
In this problem better solution will be use DataGridView.CellMouseClick
event. 在这个问题中,更好的解决方案将是使用
DataGridView.CellMouseClick
事件。 This event will fire when you only click on any cell. 只需单击任何单元格即可触发此事件。
Private Sub DataGridView1_CellMouseClick(sender as Object, e as DataGridViewCellMouseEventArgs) _
Handles DataGridView1.CellMouseClick
'todo
End Sub
You have to use DataGridView.CellValueChanged
event. 您必须使用
DataGridView.CellValueChanged
事件。 This is the best event to work with in your case. 这是在您的情况下使用的最佳事件。 It is not a good idea to use
MouseClick
event. 使用
MouseClick
事件不是一个好主意。
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged(v=vs.110).aspx
Inside the event sub use the DataGridViewCellEventArgs
property e.columnIndex
to check that the colunn edited is the one containing the checkbox. 在事件子内部使用
DataGridViewCellEventArgs
属性e.columnIndex
来检查编辑的colunn是否包含复选框。 Otherwise you have to check your code. 否则你必须检查你的代码。
it is better to use a
Using
clause when working with sqlcommand class在使用sqlcommand类时,最好使用
Using
子句
Using cmd as New SqlCommand("Update FAMPAR SET Tag = '" & DGVHelp(0, e.RowIndex).Value & "' where Account_Code = '" & DGVHelp(1, e.RowIndex).Value & "' ", con1)
ExecuteQuery(con1, "EDITCHECKBOX") ' Its my UDF
End Using
EDIT 1: 编辑1:
After checking you solution. 检查完解决方案后。 it is not so clear but you can do some workaround.
它不是那么清楚,但你可以做一些解决方法。
1- Define a boolean variable m_BoolValueChanged
1-定义布尔变量
m_BoolValueChanged
Private m_BoolValueChanged as Boolean = True
2- Use this variable in DGVHelp_CellValueChanged
Sub to prevent infinite loop. 2-在
DGVHelp_CellValueChanged
Sub中使用此变量以防止无限循环。 like to following: 喜欢以下:
Private Sub DGVHelp_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DGVHelp.CellValueChanged
Try
If PubHelpCode = "ACTMAST" And e.ColumnIndex = 0 Then
If m_BoolValueChanged Then
m_BoolValueChanged = False
cmd = New SqlCommand("Update FAMPAR SET Tag = '" & DGVHelp(0, e.RowIndex).Value & "' where Account_Code = '" & DGVHelp(1, e.RowIndex).Value & "' ", con1)
ExecuteQuery(con1, "EDITCHECKBOX")
Dim ri As Integer = DGVHelp.CurrentRow.Index
DGV_Reset()
DGVHelp.Rows(ri).Selected = True
m_BoolValueChanged = False
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
You may find better solution but this will helps 您可能会找到更好的解决方案但这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.