DataGridViewRowsAdded处理程序提供错误

时间:2014-06-24 14:19:15

标签: vb.net winforms datagridview addhandler

我尝试将RowsAddedCellFormatting处理程序添加到我的项目中。我似乎已经清除了CellFormatting处理程序中的所有错误,但我的RowsAdded正在提出一些我无法弄清楚的错误。

  

没有为参数' rowCount'指定参数' Public Sub   新建(rowIndex As Integer,rowCount As Integer)'

     

' 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。所以我将Private Sub 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

这导致了一堆新错误,所以我解除了它。为什么会导致错误?

1 个答案:

答案 0 :(得分:2)

InitializeDataGridView方法中只有一个错误

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

应该是:

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

此外,事件处理程序已通过Handles DataGridView1.RowsAddedHandles DataGridView1.CellFormatting方法末尾的OnRowAddedOnCellFormatting连接,因此您无需< em>第二次附加事件处理程序。最后不需要这两行(更正的)行:

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