![](/img/trans.png)
[英]VB.NET DataGridView -> Add a column and fill cells with data from another cell
[英]VB.NET - Fill Specific Column DataGridView from Another Column
我有2 DGV名dataListBarang
和dataListTransaksi
,我试图填补dataListTransaksi
spesific列称为nama_barang
从具体列dataListBarang
称为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
当我在DGV双击dataListBarang
,列nama_barang
(上dataListTransaksi
)将装满值从列nm_barang
上DGV dataListBarang
,但它不工作。
请帮忙。
编辑我想做的是当用户双击它时从当前行复制单元格,然后在特定列中填充第二个DataGrid(dataGrid的列中没有数据)的第一行。
注意:
您的图片显示的是右侧的空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
我根据@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并添加新行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.