简体   繁体   English

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

[英]VB.NET - Fill Specific Column DataGridView from Another Column

i have 2 DGV name dataListBarang and dataListTransaksi , i'm trying to fill dataListTransaksi spesific column called nama_barang from specific column in dataListBarang called nm_barang 我有2 DGV名dataListBarangdataListTransaksi ,我试图填补dataListTransaksi spesific列称为nama_barang从具体列dataListBarang称为nm_barang

my code so far 到目前为止我的代码

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

when i double click on DGV dataListBarang , column nama_barang (on dataListTransaksi ) will filled with value from column nm_barang on DGV dataListBarang , but its not working. 当我在DGV双击dataListBarang ,列nama_barang (上dataListTransaksi )将装满值从列nm_barang上DGV dataListBarang ,但它不工作。

please help. 请帮忙。

EDIT what i'm trying to do is copy cell from current row when user double click on it then fill the first row on second DataGrid (dataGrid has columns with no data in it) in specific column. 编辑我想做的是当用户双击它时从当前行复制单元格,然后在特定列中填充第二个DataGrid(dataGrid的列中没有数据)的第一行。

在此处输入图片说明

note: 注意:

  1. see the left dataGrid has data and the right dataGrid is empty 看到左边的dataGrid有数据,右边的dataGrid为空
  2. see the number!, when user double click, the value from 1 is copied to number 2 看到数字!,当用户双击时,值从1复制到数字2
  3. most of column in right dataGrid is manually input (except Nama Barang) and save to database 右侧data​​Grid中的大多数列都是手动输入的(Nama Barang除外)并保存到数据库

What your picture shows is an empty DataGridView on the right. 您的图片显示的是右侧的空DataGridView When you copy the cell from the left grid to the cell in the right grid as the picture shows, will have some problems. 如图所示,当您将单元格从左侧网格复制到右侧网格中的单元格时,将出现一些问题。 You will have to “Add” a new row first. 您将必须先“添加”新行。 Otherwise, subsequent double clicks from the user will simply overwrite the previous double click. 否则,来自用户的后续双击将仅覆盖先前的双击。 The code below checks to see if the double click came from column 0 and if so simply adds a new row to the grid on the right. 下面的代码检查双击是否来自第0列,如果是,只需在右侧的网格中添加一个新行。 The value in column 0 is copied to the new row column 1. Hope I am not missing something. 列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

Update: If you do not know which column to copy but you know its name.. 更新:如果您不知道要复制哪一列,但知道其名称。

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

i modified my code based on answer by @JhonG 我根据@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

when i double click in firstDataGrid it copy the value to another dataGrid and add new row. 当我双击firstDataGrid时,它将值复制到另一个dataGrid并添加新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM