2

RowsAddedプロジェクトにおよびCellFormattingハンドラを追加しようとしています。CellFormattingハンドラーのすべてのエラーを解決したようですが、RowsAdded理解できないエラーがいくつか発生しています。

「Public Sub New(rowIndex As Integer, rowCount As Integer)」のパラメーター「rowCount」に引数が指定されていません

'Integer' はデリゲート型ではないため、'AddressOf' 式を '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() の 2 行を削除しました。ただし、答えには、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

これにより、一連の新しいエラーが発生したため、元に戻しました。なぜそれがエラーを引き起こすのですか?

4

1 に答える 1

2

メソッドには 1 つだけタイプミスがあります。InitializeDataGridView

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

次のようにする必要があります。

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

また、イベント ハンドラーはandメソッドの最後にあるHandles DataGridView1.RowsAddedandを介して既に接続されているため、イベント ハンドラーを再度アタッチする必要はありません。これらの 2 つの (修正された) 行は最終的に不要になります。Handles DataGridView1.CellFormattingOnRowAddedOnCellFormatting

AddHandler Me.DataGridView1.CellFormatting, New DataGridViewCellFormattingEventHandler(AddressOf OnCellFormatting)
AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf OnRowsAdded)
于 2014-06-24T14:32:27.360 に答える