单击复选框并将所选行传递到另一个 datagridview

Click on checkbox and pass selected row to another datagridview

提问人:kenny 提问时间:4/9/2022 最后编辑:kenny 更新时间:4/9/2022 访问量:54

问:

当我从 datagridview 2 中选择超过 2 行时,我在更新或替换 datagridview 2 中找到的行时遇到问题

为了显示 datagriview 1 和 datagridview 2 中的信息,我使用数据源进行如下操作

数据网格视图 1:

Dim dtsm As New DataTable
dtsm = negPern.Listar_Personalxempresa(VGlobales.Base, Me.cboempresa.SelectedValue)
Me.dgvListadoMarcador.DataSource = dtsm
Me.dgvListadoMarcador.Columns(0).ReadOnly = False

数据网格视图 2:

    Dim dts2 As New DataTable
    dtsm = negPern.Listar_Personalxempresa(VGlobales.Base, Me.cboempresa.SelectedValue)
    Me.dgv1.DataSource = dtsm
    Me.dgv1.Columns(0).ReadOnly = False

这反映如下

enter image description here

更新按钮的过程是让用户选择 DGV 2 数据网格视图的任何行,如果它存在于数据网格视图 1 或 DGV 1 中,这将更新它

到目前为止,我已经设法通过使用以下代码逐个选择 1 来做到这一点

 Dim rownu As DataRow = dtsm.NewRow()
    Dim rowSelectedM As List(Of DataGridViewRow) = New List(Of DataGridViewRow)()


    Try
        For Each row As DataGridViewRow In dgv2.Rows
            Dim cellSelecion As DataGridViewCheckBoxCell = TryCast(row.Cells("ELEGIR"), DataGridViewCheckBoxCell)

            If Convert.ToBoolean(cellSelecion.Value) Then
                rowSelectedM.Add(row)
            End If
        Next


        For Each rown As DataGridViewRow In dgv1.Rows
            If Convert.ToString(rown.Cells(3).Value).Equals(dgv2.CurrentRow.Cells(2).Value) Then
                existeU = True
                fila = rown.Cells(3).RowIndex
            End If
        Next


        For Each row As DataGridViewRow In rowSelectedM
            If existeU = True Then
                If MessageBox.Show("ESTE REGISTRO YA EXISTE EN LA BASE MARCADOR N° FILA :  " + " " + fila.ToString + " " + "DESEA ACTUALIZAR ?", "AUTONORT S.A.", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    'AQUI VA EL DTS


                    rownu("ACCION") = "U"
                    rownu("IDEMPRESA") = row.Cells(1).Value
                    rownu("IDCODIGOGENERAL") = row.Cells(2).Value
                    rownu("IDPLANILLA") = row.Cells(3).Value
                    rownu("FECHA_INICIOPLANILLA") = row.Cells(4).Value
                    rownu("A_MATERNO") = row.Cells(5).Value
                    rownu("A_PATERNO") = row.Cells(6).Value
                    rownu("NOMBRES") = row.Cells(7).Value
                    rownu("NRODOCUMENTO") = row.Cells(8).Value
                    rownu("LIQUIDADO") = row.Cells(9).Value
                    rownu("FECHA_INGRESO") = row.Cells(10).Value
                    rownu("FECHA_CESE") = row.Cells(11).Value
                    rownu("IDNACIONALIDAD") = row.Cells(12).Value
                    rownu("IDSUCURSAL") = row.Cells(13).Value
                    rownu("SUC") = row.Cells(14).Value



                    dtsm.Rows.RemoveAt(fila)
                    dtsm.Rows.InsertAt(rownu, fila)

                    dgv1.ClearSelection()


                    dgv1.FirstDisplayedScrollingRowIndex = dtsm.Rows.IndexOf(rownu)
                    dgv1.Focus()


                End If

            End If



        Next
  

当我选择超过 2 行时,出现以下错误

删除第 2 行

理想的情况是替换 datagridview 的 2 个选定行,并添加操作 U = update

enter image description here

.NET vb.net visual-studio-2010

评论

1赞 user18387401 4/9/2022
首先,为什么要创建新对象并立即丢弃它们? 是 的简写。如果您只是要在下一行为变量分配其他变量,为什么还要创建一个新的变量并将其分配给变量?除非需要新对象,否则不要在声明变量时创建新对象。只需这样做: .变量类型将从表达式中推断出来。DataTableDim dtsm As New DataTableDim dtsm As DataTable = New DataTableDataTableDataTableDim dtsm = negPern.Listar_Personalxempresa(VGlobales.Base, Me.cboempresa.SelectedValue)
0赞 user18387401 4/9/2022
接下来,你为什么要使用?重点是,如果强制转换失败,它不会抛出异常,而是返回。你不测试,因为你知道投射会成功,所以不要使用 .请改用。也就是说,无论如何,强制转换是没有意义的,因为你从不使用你强制转换的类型的任何成员。您使用的只是 ,无论如何它都是基类的成员。TryCastTryCastNothingNothingTryCastDirectCastValue
1赞 user18387401 4/9/2022
至于这个问题,我不知道第二个循环应该实现什么,但它看起来有问题。您在该循环中设置了两个变量,但这些变量每个变量只能有一个值。您似乎希望它们为每一行提供单独的值,但很难说,因为您没有费心解释,并且代码中没有注释。

答: 暂无答案