2017-04-01 50 views
0

我有2 DGV名dataListBarangdataListTransaksi,我試圖填補dataListTransaksi spesific列dataListBarang稱爲nm_barangVB.NET - 從另一列填寫特定的列的DataGridView

到目前爲止我的代碼

稱爲 nama_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,但它不能正常工作ING。

請大家幫忙。

編輯 我想要做的是從當前行復制單元格時,它的用戶雙擊即可填寫具體列在第二個DataGrid的第一行(的數據網格有沒有數據列在裏面)。

enter image description here

注:

  1. 看到左側的數據網格具有數據和正確的數據網格是空的
  2. 看到的數字!當用戶雙擊,值從1被複制到數2
  3. 右側data​​Grid中的大部分列都是手動輸入的(Nama Barang除外)並保存到數據庫
+0

你想複製整列或只是單元格嗎?另外,網格數據是否被綁定?設置目標列DataPropertyName ...顯然不起作用。 – JohnG

+0

是的,我試圖在列複製細胞,第一DGV('dataListBarang')是結合的數據(從表中讀出),但是第二DGV('dataTransaksiBarang')不是,因爲第二DGV是手動輸入(插入到桌子)。 – reynd

+0

如果您正在複製單個單元格...您將在何處將此單元格複製到另一個網格中?例如,用戶在第一個網格中雙擊第3行第3列中的單元格...是否將該單元格複製到另一個網格中的同一行和列中? – JohnG

回答

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 
+0

感謝@JhonG,我根據你的答案提出了我的解決方案。 – reynd

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並添加新行。

+0

我可能在這裏是錯誤的......但檢查是否「e」的行或列小於零是不必要的,因爲在網格外雙擊不會觸發此事件。你幾乎可以保證這裏有效的單元格,即使新行雙擊也不會觸發這個事件。只是一個想法。 – JohnG