2012-03-01 52 views
0
 Dim datatable_default_view As DataTable = _datatable.DefaultView.ToTable 
     Dim servicenumber As String = datatable_default_view.Rows.Item(e.RowIndex)("Service Number").ToString 
'gets the service number of the selected row 

     If _datatable.PrimaryKey.Length = 0 Then 
      Dim keys(0) As DataColumn 
      keys(0) = _datatable.Columns("Service Number") 
      _datatable.PrimaryKey = keys 
     End If 

     Dim datarow_edited As DataRow = _datatable.Rows.Find(servicenumber) 

     Try 
      MsgBox("Record" + datarow_edited("service number") + " was edited") 
     Catch ex As Exception 
      MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error") 
     End Try 

我已經把這個代碼DatagridView_CellEndEdit事件datatable.Rows.Find不起作用

的問題下:
離開小區時,「data_row編輯的」變量不被初始化編輯模式使用鍵盤(即:按下下/上箭頭)。 如果我使用鼠標,它按預期工作。爲什麼是這樣 ?這是一個已知的錯誤,我正在使用VB.net 2008.請幫助

我上傳了一個示例項目示例項目here。如果可能,請檢查它。

回答

1

剛剛在VS 2010上試用過您的示例解決方案,它的工作方式如預期。使用箭頭鍵切換行時,DataRow也會正確初始化。

編輯:

你現在得到,抱歉。

看起來像幕後的一些異步問題。

也許處理數據表本身的RowChanged事件可能是你

Imports System.Data.OleDb 
Public Class Form1 
    Private _datatable As DataTable 
    Dim conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Members Database.mdb;Persist Security Info=False" 


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Dim connection As New OleDbConnection(conn) 
     Dim adapter As OleDbDataAdapter 


     Dim _branchname As String = "A BAUR" 
     Dim cmd As String = "Select [Service Number] ,[Name], [Gender], [Subscription] from " + _branchname.Insert(0, "[") + "]" 
     adapter = New OleDbDataAdapter(cmd, conn) 
     _datatable = New DataTable 
     adapter.Fill(_datatable) 


     If _datatable.PrimaryKey.Length = 0 Then 

      Dim keys(0) As DataColumn 
      keys(0) = _datatable.Columns("Service Number") 
      _datatable.PrimaryKey = keys 
     End If 


     AddHandler _datatable.RowChanged, AddressOf _datatable_RowChanged 

     DataGridView1.DataSource = _datatable 

    End Sub 


    Private Sub _datatable_RowChanged(sender As Object, e As System.Data.DataRowChangeEventArgs) 

     Dim servicenumber = e.Row("Service Number") 

     Dim datarow_edited As DataRow = _datatable.Rows.Find(servicenumber) 

     Try 
      MsgBox("Record" + datarow_edited("service number") + " was edited") 
     Catch ex As Exception 
      MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error") 
     End Try 
    End Sub 

End Class 
+0

我在XP上使用VS 2008(也試過7)。我做了這個[** clip **](https://skydrive.live.com/redir.aspx?cid=923085ae74a2e362&resid=923085AE74A2E362!302&parid = 923085AE74A2E362!300&authkey =!AHGVjqhn6ODlJRo),讓你知道發生了什麼。注意,當我退出使用鍵盤(第二次嘗試)時,它給出'對象引用未設置爲對象的實例'錯誤 – gawicks 2012-03-02 17:31:40

+0

它工作!謝謝Man.You Rock! – gawicks 2012-03-03 10:17:47

0
Dim servicenumber As String = _datatable.Rows(e.RowIndex)("Service Number").ToString() 
+0

都能跟得上一種解決方法。這似乎並沒有工作 – gawicks 2012-03-01 16:51:23

+0

我使用DefaultView,因爲我讓用戶使用bindingSource過濾數據。只需使用_datatable.Rows(e.RowIndex)給出過濾表的RowIndex而不是實際的表:) – gawicks 2012-03-01 17:01:25