DataGridView值从另一个DataGridView删除
我有两个DataGridViews。一个是dtgridpopulation,并带有来自数据库的数据。
DTGRIDPEPULATE列为(复选框,代码,名称)
复选框 | 代码 | 名称 |
---|---|---|
复选框ICON | C1 | custerut_one |
,第二个DataGridView是dtgridgenerate,它从DTGRIDPOPULATE产生了值。 我使用代码dtgridpopulate.Rows.Add(代码,名称)
将从dtgridpopulate的值添加到dtgridgenerate。
dtgridgenerate列是(代码,名称)
代码 | 名称 |
---|---|
c1 | customer_one |
当我在dtgridpopulate中选中复选框时,它将将值(代码,名称)传输到dtgridgenerate。但是问题是当我取消选中DTgridPopulate中的复选框时,它也应删除 dtgridgenere中的值。
Private Sub dtgridPopulateSelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dtgridPopulate.CurrentCellDirtyStateChanged
RemoveHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
If TypeOf dtgridPopulate.CurrentCell Is DataGridViewCheckBoxCell Then
dtgridPopulate.EndEdit()
Dim Checked As Boolean = CType(dtgridPopulate.CurrentCell.Value, Boolean)
If Checked Then
code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
dtgridGenerate.Rows.Add(code, name)
Else
For Each drow As DataGridViewRow In dtgridPopulate.SelectedRows 'This is for uncheck but it doens't work
dtgridGenerate.Rows.Remove(row)
Next
End If
End If
AddHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
End Sub
我是指此参考
当我取消选中复选框时错误:提供的行不属于此DataGridView控件。参数名称:DataGridViewRow
I have two datagridviews. One is dtgridPopulate and is populated with data from the database.
dtgridPopulate columns are (checkbox, code, name)
checkbox | code | name |
---|---|---|
checkbox icon | c1 | customer_one |
Then the second datagridview is dtgridGenerate which has generated values from dtgridPopulate.
I use the code dtgridPopulate.Rows.Add(code, name)
to add manually the value from dtgridPopulate to dtgridGenerate.
dtgridGenerate columns are (code, name)
code | name |
---|---|
c1 | customer_one |
When I check the checkbox in the dtgridPopulate it will transfer the values (code, name) to the dtgridGenerate. But the problem is when I uncheck the checkbox in the dtgridPopulate, It should also REMOVE the values in the dtgridGenerate.
Private Sub dtgridPopulateSelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dtgridPopulate.CurrentCellDirtyStateChanged
RemoveHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
If TypeOf dtgridPopulate.CurrentCell Is DataGridViewCheckBoxCell Then
dtgridPopulate.EndEdit()
Dim Checked As Boolean = CType(dtgridPopulate.CurrentCell.Value, Boolean)
If Checked Then
code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
dtgridGenerate.Rows.Add(code, name)
Else
For Each drow As DataGridViewRow In dtgridPopulate.SelectedRows 'This is for uncheck but it doens't work
dtgridGenerate.Rows.Remove(row)
Next
End If
End If
AddHandler dtgridPopulate.CurrentCellDirtyStateChanged, AddressOf dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
End Sub
I refer to this reference
Error when I uncheck the checkbox: row provided does not belong to this datagridview control. parameter name: datagridviewrow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您已取消订阅,然后重新订阅该活动。通常,仅当事件中的代码“更改”网格中的某些内容会导致事件重新发生,并且当前代码中不会发生这种情况。也没有必要结束网格编辑。
此外,您使用网格
CurrentCellDirtyStateChanged
事件具有与您要做的事情有关的一个缺点……如果用户一遍又一遍地检查并取消选中相同的单元格,则不会重新开火。换句话说,如果用户单击一个复选框单元格,并且复选框将检查并触发事件……然后……如果用户在单击任何其他单元格之前“取消选中”相同的单元格,则事件将不会添加。另外,该事件看起来与事件的签名使用了不同的网格……
dtgridpopulateselectall_currentcelrentcelldirtystatatatechanged
…? …“ selectall”和此事件的网格称为…dtgridpepulate
…这很困惑。鉴于此,我建议两件事可以提供帮助。 1)为第二个网格创建并使用“空”
DataTable
。这样,我们可以简单地添加和删除该表中的项目。 2)为此使用网格cellContnetClick
事件。如果用户仅在更改复选框时启动,则将重新开火。因此,我提出了这些小变化。首先将名为
GeneratedT
的全局变量添加为dataTable
用作dataSource
的全局变量。最初,该表将是空的。第二,使用网格cellContentClick
事件进行这些添加并删除行。然后,在单元内容单击事件中,如果单击单元格是复选框单元格,则…抓取检查状态,
code
和name
of Clicked-On行。然后,如果检查了复选框单元格,则只需将代码
和name
添加到第二个网格globals globaldataTable
variable generatedt 代码>。如果未选中复选框,那么我们将在第二个网格中的每一行中循环,如果我们找到匹配项,则只需从第二个网格globals globalsdataTable
…类似…类似…的第二行删除该行。I am not sure “why” you have unsubscribed and then re-subscribed to the event. Typically, this is only needed if the code in the event “changes” something in the grid that would cause the event to re-fire and this is not happening in your current code. Nor is ending the grids edit necessary.
Also, your use of the grids
CurrentCellDirtyStateChanged
event has one drawback in relation to what you want to do… it will NOT re-fire if the user checks and unchecks the same cell over and over. In other words, if the user clicks a check box cell and the check box becomes checked and the event fires… then … if the user “unchecks” the same cell before clicking on any other cell, then the event will NOT refire.Also, it looks like the event is using a different grid from the signature of the event…
dtgridPopulateSelectAll_CurrentCellDirtyStateChanged
… ? … “SelectAll” and the grid in the event is called…dtgridPopulate
… this is confusing.Given this, I suggest two things to help. 1) create and use an “Empty”
DataTable
for the second grid. This way we could simply add and remove items from that table. 2) use the gridsCellContnetClick
event for this. It will re-fire if the user clicks on the same cell over and over in addition to firing ONLY when the check box is changed.So, I propose these small changes. First add a global variable named
generateDT
as aDataTable
used as aDataSource
to the second grid. Initially this table would be empty. Second use the gridsCellContentClick
event to make these additions and removal of the rows.Then in the cell content click event, if the clicked cell is a check box cell, then… grab the checked state and both the
code
andname
of the clicked-on row. Then if the check box cell is checked, then simply add thecode
andname
to the second grids globalDataTable
variablegenerateDT
. If the check box is unchecked, then we will loop through each row in the second grid and if we find a match then simply remove that row from the second grids globalDataTable
… something like…