DataGridView值从另一个DataGridView删除

发布于 2025-01-30 12:15:46 字数 2218 浏览 3 评论 0原文

我有两个DataGridViews。一个是dtgridpopulation,并带有来自数据库的数据。

DTGRIDPEPULATE列为(复选框,代码,名称)

复选框代码名称
复选框ICONC1custerut_one

,第二个DataGridView是dtgridgenerate,它从DTGRIDPOPULATE产生了值。 我使用代码dtgridpopulate.Rows.Add(代码,名称)将从dtgridpopulate的值添加到dtgridgenerate。

dtgridgenerate列是(代码,名称)

代码名称
c1customer_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)

checkboxcodename
checkbox iconc1customer_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)

codename
c1customer_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2025-02-06 12:15:46

我不确定您已取消订阅,然后重新订阅该活动。通常,仅当事件中的代码“更改”网格中的某些内容会导致事件重新发生,并且当前代码中不会发生这种情况。也没有必要结束网格编辑。

此外,您使用网格CurrentCellDirtyStateChanged事件具有与您要做的事情有关的一个缺点……如果用户一遍又一遍地检查并取消选中相同的单元格,则不会重新开火。换句话说,如果用户单击一个复选框单元格,并且复选框将检查并触发事件……然后……如果用户在单击任何其他单元格之前“取消选中”相同的单元格,则事件将不会添加。

另外,该事件看起来与事件的签名使用了不同的网格……dtgridpopulateselectall_currentcelrentcelldirtystatatatechanged…? …“ selectall”和此事件的网格称为…dtgridpepulate…这很困惑。

鉴于此,我建议两件事可以提供帮助。 1)为第二个网格创建并使用“空” DataTable。这样,我们可以简单地添加和删除该表中的项目。 2)为此使用网格cellContnetClick事件。如果用户仅在更改复选框时启动,则将重新开火。

因此,我提出了这些小变化。首先将名为GeneratedT的全局变量添加为dataTable用作dataSource的全局变量。最初,该表将是空的。第二,使用网格cellContentClick事件进行这些添加并删除行。

Dim generateDT As DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Dim dt As DataTable = New DataTable()
  dt.Columns.Add("checkbox", GetType(Boolean))
  dt.Columns.Add("code", GetType(String))
  dt.Columns.Add("Name", GetType(String))
  dt.Rows.Add(False, "c1", "customer 1")
  dt.Rows.Add(False, "c2", "customer 2")
  dt.Rows.Add(False, "c3", "customer 3")
  dtgridPopulate.DataSource = dt
  generateDT = New DataTable()
  generateDT.Columns.Add("code", GetType(String))
  generateDT.Columns.Add("Name", GetType(String))
  dtgridGenerate.DataSource = generateDT
End Sub

然后,在单元内容单击事件中,如果单击单元格是复选框单元格,则…抓取检查状态,codename of Clicked-On行。然后,如果检查了复选框单元格,则只需将代码name添加到第二个网格globals global dataTable variable generatedt 代码>。如果未选中复选框,那么我们将在第二个网格中的每一行中循环,如果我们找到匹配项,则只需从第二个网格globals globals dataTable…类似…类似…的第二行删除该行。

Private Sub dtgridPopulate_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dtgridPopulate.CellContentClick
  If e.ColumnIndex = 0 Then
    Dim Checked = CType(dtgridPopulate.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue, Boolean)
    Dim code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
    Dim Name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
    If Checked Then
      generateDT.Rows.Add(code, Name)
    Else
      Dim rowToRemove As DataRowView
      For Each row As DataGridViewRow In dtgridGenerate.Rows
        If row.Cells(0).Value.ToString() = code Then
          rowToRemove = CType(row.DataBoundItem, DataRowView)
          generateDT.Rows.Remove(rowToRemove.Row)
          Exit For
        End If
     Next
    End If
  End If
End Sub

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 grids CellContnetClick 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 a DataTable used as a DataSource to the second grid. Initially this table would be empty. Second use the grids CellContentClick event to make these additions and removal of the rows.

Dim generateDT As DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Dim dt As DataTable = New DataTable()
  dt.Columns.Add("checkbox", GetType(Boolean))
  dt.Columns.Add("code", GetType(String))
  dt.Columns.Add("Name", GetType(String))
  dt.Rows.Add(False, "c1", "customer 1")
  dt.Rows.Add(False, "c2", "customer 2")
  dt.Rows.Add(False, "c3", "customer 3")
  dtgridPopulate.DataSource = dt
  generateDT = New DataTable()
  generateDT.Columns.Add("code", GetType(String))
  generateDT.Columns.Add("Name", GetType(String))
  dtgridGenerate.DataSource = generateDT
End Sub

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 and name of the clicked-on row. Then if the check box cell is checked, then simply add the code and name to the second grids global DataTable variable generateDT. 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 global DataTable… something like…

Private Sub dtgridPopulate_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dtgridPopulate.CellContentClick
  If e.ColumnIndex = 0 Then
    Dim Checked = CType(dtgridPopulate.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue, Boolean)
    Dim code = dtgridPopulate.CurrentRow.Cells(1).Value.ToString
    Dim Name = dtgridPopulate.CurrentRow.Cells(2).Value.ToString
    If Checked Then
      generateDT.Rows.Add(code, Name)
    Else
      Dim rowToRemove As DataRowView
      For Each row As DataGridViewRow In dtgridGenerate.Rows
        If row.Cells(0).Value.ToString() = code Then
          rowToRemove = CType(row.DataBoundItem, DataRowView)
          generateDT.Rows.Remove(rowToRemove.Row)
          Exit For
        End If
     Next
    End If
  End If
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文