我正在尝试将RowsAdded
andCellFormatting
处理程序添加到我的项目中。我似乎已经清除了CellFormatting
处理程序中的所有错误,但我RowsAdded
给出了一些我无法弄清楚的错误。
未为“Public Sub New(rowIndex As Integer, rowCount As Integer)”的参数“rowCount”指定参数
“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
这导致了一堆新的错误,所以我撤消了它。为什么这会导致错误?