I'm trying to add the RowsAdded
and CellFormatting
handlers to my project. I seem to have cleared up all errors in the CellFormatting
handler but my RowsAdded
is giving some errors that I cannot figure out.
Argument not specified for parameter 'rowCount' of 'Public Sub New(rowIndex As Integer, rowCount As Integer)'
'AddressOf' expression cannot be converted to 'Integer' because 'Integer' is not a delegate type
My code:
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
And
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
In regards to the errors, I'm not using rowCount anywhere so maybe I need to?
Why does it think I using integer as a delegate type?
I checked and I don't have any public variable rowCount or rowIndex.
Per the answer I deleted the two lines in Sub InitializeDataGridView() which seems to fix my errors. However the answer also states that Args should be Handler. So I changed the Private Sub OnRowsAdded to
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
Which caused a bunch of new errors, so I undid it. Why does that cause errors though?
There is only one typo in the InitializeDataGridView
method:
AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventArgs(AddressOf OnRowsAdded)
Should be:
AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf OnRowsAdded)
^^^^^^
Also, the event handler is already connected via the Handles DataGridView1.RowsAdded
and Handles DataGridView1.CellFormatting
at the end of your OnRowAdded
and OnCellFormatting
methods, so you don't need to attach the event handler a second time. These two (corrected) lines are finally unnecessary:
AddHandler Me.DataGridView1.CellFormatting, New DataGridViewCellFormattingEventHandler(AddressOf OnCellFormatting)
AddHandler Me.DataGridView1.RowsAdded, New DataGridViewRowsAddedEventHandler(AddressOf OnRowsAdded)