2014-06-24 39 views
2

我正在嘗試將RowsAddedCellFormatting處理程序添加到我的項目中。我似乎清除了CellFormatting處理程序中的所有錯誤,但是我的RowsAdded正在給出一些我無法弄清楚的錯誤。爲參數沒有指定的「rowCount時」「公用Sub 新(rowIndex位置作爲整數,rowCount時作爲整數)」DataGridViewRowsAdded處理程序給出錯誤

參數

「AddressOf」表達不能被轉換爲「整數」,因爲「整數」是不是委託類型

我的代碼:

Private Sub InitializeDataGridView() 
    Try 
     ' Set up the DataGridView. 
     With Me.DataGridView1 
      ' Automatically generate the DataGridView columns. 
      .AutoGenerateColumns = True 

      ' Set up the data source. 
      .DataSource = dt 

      ' Automatically resize the visible rows. 
      .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders 

      ' Set the DataGridView control's border. 
      .BorderStyle = BorderStyle.Fixed3D 

      ' Put the cells in edit mode when user enters them. 
      .EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2 

      ' Disables Add New Row 
      .AllowUserToAddRows = False 

      '.AllowUserToOrderColumns = False 
      For Each column As DataGridViewColumn In DataGridView1.Columns 
       column.SortMode = _ 
       DataGridViewColumnSortMode.Programmatic 
      Next 

      AddHandler Me.DataGridView1.CellFormatting, New DataGridViewCellFormattingEventHandler(AddressOf OnCellFormatting) 
      AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventArgs(AddressOf OnRowsAdded) 

     End With 

    Catch ex As SqlException 
     MessageBox.Show(ex.ToString, _ 
      "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     System.Threading.Thread.CurrentThread.Abort() 
    End Try 
End Sub 

而且

Private Sub OnCellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting 
    'If e.ColumnIndex = DataGridView1.Columns("Contact").Index Then 
    ' e.FormattingApplied = True 
    ' Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex) 
    ' e.Value = String.Format("{0} : {1}", row.Cells("ContactName").Value, row.Cells("Phone").Value) 
    'End If 
End Sub 
Private Sub OnRowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded 
    'For i As Integer = 0 To e.RowIndex - 1 
    ' Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex + i) 
    ' row.Cells("Contact").Value = String.Format("{0} : {1}", row.Cells("ContactName").Value, row.Cells("Phone").Value) 
    'Next 
End Sub 

關於錯誤,我沒有在任何地方使用rowCount,所以也許我需要?

它爲什麼認爲我使用整數作爲委託類型?

我檢查了,我沒有任何公共變量rowCount或rowIndex。


根據答案我刪除了Sub InitializeDataGridView()中的兩行,這似乎解決了我的錯誤。但答案還表明,Args應該是Handler。所以我改變了私人小組OnRowsAdded

Private Sub OnRowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventHandler) Handles DataGridView1.RowsAdded 
    For i As Integer = 0 To e.RowIndex - 1 
     Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex + i) 
     row.Cells("Contact").Value = String.Format("{0} : {1}", row.Cells("ContactName").Value, row.Cells("Phone").Value) 
    Next 
End Sub 

這導致了一堆新的錯誤,所以我解開了它。爲什麼這會導致錯誤?

回答

2

只有一個錯字InitializeDataGridView方法:

AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventArgs(AddressOf OnRowsAdded) 

應該是:

AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf OnRowsAdded) 
                     ^^^^^^ 

而且,該事件處理程序已經經由Handles DataGridView1.RowsAddedHandles DataGridView1.CellFormatting在的端部連接你的OnRowAddedOnCellFormatting方法,所以你不需要附加事件處理程序a第二次。這兩個(修正)線終於不必要的:

AddHandler Me.DataGridView1.CellFormatting, New DataGridViewCellFormattingEventHandler(AddressOf OnCellFormatting) 
AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf OnRowsAdded) 
+0

我刪除了這兩個不必要的行,它是固定的! onrowsadded的私人子目錄仍然需要Args而不是Handler,否則會出現更多錯誤。這可以解釋嗎? – ZL1Corvette

+0

@ ZL1Corvette除非我不明白你的問題,答案是在我的答案的第一部分(錯字Args vs Handler)。 – Chris

+0

我在Sub OnRowAdded中將Args更改爲Handler並出現錯誤。原始帖子中的細節。 – ZL1Corvette