2014-06-24 40 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