VB.NET - 从另一列填充特定列DataGridView

时间:2017-04-01 05:54:10

标签: vb.net datagridview

我有2个DGV名称dataListBarangdataListTransaksi,我正在尝试从名为dataListTransaksi的{​​{1}}中的特定列填充名为nama_barang的{​​{1}}特定列{1}}

我的代码到目前为止

dataListBarang

当我双击DGV nm_barang时,Private Sub dataListBarang_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataListBarang.CellContentDoubleClick If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then namaBarang = Me.dataListBarang.CurrentRow.Cells(0).Value.ToString dataListTransaksi.Columns(1).DataPropertyName = namaBarang End If dataListBarang.Refresh() End Sub dataListBarang将填充DGV nama_barang上的列dataListTransaksi的值,但是不工作。

请帮忙。

修改 我正在尝试做的是当用户双击它时从当前行复制单元格然后在特定列中填充第二个DataGrid上的第一行(dataGrid具有其中没有数据的列)。

enter image description here

请注意:

  1. 看左边的dataGrid有数据,右边的dataGrid是空的
  2. 查看数字!,当用户双击时,1中的值被复制到数字2
  3. 右侧data​​Grid中的大部分列是手动输入的(Nama Barang除外)并保存到数据库

2 个答案:

答案 0 :(得分:0)

图片显示的是右边的空DataGridView。如图所示,当您将单元格从左侧网格复制到右侧网格中的单元格时,会出现一些问题。您必须先“添加”新行。否则,来自用户的后续双击将简单地覆盖之前的双击。下面的代码检查双击是否来自第0列,如果是这样,只需在右侧的网格中添加一个新行。第0列中的值被复制到新行第1列。希望我没有错过任何内容。

Private Sub dataListBarang_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataListBarang.CellContentDoubleClick
  Dim row As Integer = e.RowIndex
  Dim col As Integer = e.ColumnIndex
  If (col = 0) Then
    dataListTransaksi.Rows.Add("", dataListBarang.Rows(row).Cells(0).Value)
  End If
End Sub

更新:如果您不知道要复制哪个列,但您知道其名称..

Private Sub dataListBarang_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataListBarang.CellContentDoubleClick
  Dim row As Integer = e.RowIndex
  Dim col As Integer = e.ColumnIndex
  If (col = 0) Then
    Dim newRowIndex As Integer = dataListTransaksi.Rows.Add()
    dataListTransaksi.Rows(newRowIndex).Cells("MyColName").Value = dataListBarang.Rows(row).Cells(0).Value
  End If
End Sub

答案 1 :(得分:0)

我根据@JhonG

的回答修改了我的代码
 Private Sub dataListBarang_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dataListBarang.CellContentDoubleClick
        Dim row As Integer = e.RowIndex
        Dim col As Integer = e.ColumnIndex
        namaBarang = Me.dataListBarang.CurrentRow.Cells(0).Value.ToString
        If (col = 0) Then

            If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
                dataListTransaksi.Rows.Add("", namaBarang)
                'hargaBarang = Me.dataListBarang
                'Me.dataListTransaksi.CurrentRow.Cells(1).Value = namaBarang
            End If
        End If

当我在firstDataGrid中双击它时,它将值复制到另一个dataGrid并添加新行。