我有一个名为 UIGrid 的控件,它是从 DataGridView
继承
的网格的 DataSource,它添加 UIGridRow 对象而不是 DataGridViewRow
我怎样才能做到这一点???
注意:该控件用于 45 项目的解决方案中,因此我无法更改为第三方解决方案
这是一个示例代码:
Public Class UIGrid : Inherits DataGridView
Private _Rows As UIGridRowCollection
Public Overloads ReadOnly Property Rows As UIGridRowCollection
Get
If _Rows Is Nothing Then
_Rows = New UIGridRowCollection(Me)
End If
Return _Rows
End Get
End Property
End Class
Public Class UIGridRow : Inherits DataGridViewRow
Public Property ChildControl As Control
End Class
Public Class UIGridRowCollection : Inherits DataGridViewRowCollection
Public Sub New(grid As UIGrid)
MyBase.New(CType(grid, DataGridView))
End Sub
Public Overloads Function Add(row As UIGridRow) As Integer
Return MyBase.Add(row)
End Function
End Class
现在,当我尝试使用 UIGrid 控件并向其添加行时,我遇到了错误:
提供的行索引超出范围。示例代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.UiGrid1.Columns.Add("Id", "Id")
Me.UiGrid1.Columns.Add("Name", "Name")
For i As Integer = 1 To 10
Dim row As New UIGridRow
row.CreateCells(Me.UiGrid1)
row.Cells(0).Value = i
row.Cells(1).Value = "Employee " & i.ToString
Me.UiGrid1.Rows.Add(row)
Next
End Sub
当我为网格设置 DataSource 属性时, Rows 属性仍然计数为 0,似乎网格没有使用重载的 Rows 属性
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("Id", GetType(Integer))
dt.Columns.Add("Name", GetType(String))
For i As Integer = 1 To 10
dt.Rows.Add(i, "Name " & i)
Next
Me.UiGrid1.DataSource = dt
End Sub
我需要当我使用 DataSource 属性时,网格使用重载的 Rows 属性并添加 UIGridRow 类型的行而不是 DataGridViewRow