【问题标题】:DataGridViewRowsAdded handler gives errorsDataGridViewRowsAdded 处理程序给出错误
【发布时间】:2014-08-14 20:34:36
【问题描述】:

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

没有为“Public Sub”的参数“rowCount”指定参数 New(rowIndex As Integer, rowCount As Integer)'

“AddressOf”表达式无法转换为“Integer”,因为“Integer”不是委托类型

我的代码:

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

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

【问题讨论】:

    标签: vb.net winforms datagridview addhandler


    【解决方案1】:

    InitializeDataGridView 方法中只有一个错字

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

    应该是:

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

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

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

    【讨论】:

    • 我删除了两个不必要的行并修复了!添加的私有子行仍然需要 Args 而不是 Handler,否则会出现更多错误。可以解释一下吗?
    • @ZL1Corvette 除非我不明白你的问题,否则答案在我答案的第一部分(错别字 Args vs Handler)。
    • 我在 Sub OnRowAdded 中将 Args 更改为 Handler 并出现错误。原帖中的详细信息。
    • @ZL1Corvette 好的,知道了。您需要将 InitializeDataGridView 方法中的...Args 更改为...Handler onlyPrivate Sub OnRowsAdded(ByVal sender As Object, ByVal e As DataGridViewRowsAddedEventArgs) 方法在编辑前就OK了。
    猜你喜欢
    • 2022-11-30
    • 2021-08-03
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多