1

我有一个名为 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

4

1 回答 1

0

我实际上面临着同样的问题......我像这样解决了(也许不优雅)......不确定是否可以使用数据源属性 - 未经测试

Imports System.Windows.Forms
Public Class myGrid : Inherits DataGridView
    Public Overloads ReadOnly Property MyRows As List(Of UIGridRow)
        Get
            Dim oLst As New List(Of UIGridRow)
            For Each ORow As DataGridViewRow In MyBase.Rows
                oLst.Add(CType(ORow, UIGridRow))
            Next
            MyRows = oLst
        End Get
    End Property
    Public Overloads ReadOnly Property MySelectedRows As List(Of UIGridRow)
        Get
            Dim oLst As New List(Of UIGridRow)
            For Each oRow As DataGridViewRow In MyBase.SelectedRows
                oLst.Add(CType(oRow, UIGridRow))
            Next
            MySelectedRows = oLst
        End Get
    End Property
    Public Class UIGridRow : Inherits DataGridViewRow
        Public Property Checked As Boolean
    End Class
    Public Overloads ReadOnly Property CurrentRow As UIGridRow
        Get
            Return CType(MyBase.CurrentRow, UIGridRow)
        End Get
    End Property
End Class

并在测试表上:

Imports myGrid.myGrid
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles     MyBase.Load
        MyGrid1.Columns.Add("Id", "Id")
        MyGrid1.Columns.Add("Name", "Name")
        For i As Integer = 1 To 10
            Dim row As New myGrid.myGrid.UIGridRow
            row.CreateCells(MyGrid1)
            row.Cells(0).Value = i
            row.Cells(1).Value = "Employee " & i.ToString
            row.Checked = (i < 5)
            MyGrid1.Rows.Add(row)
        Next
    End Sub
    Private Sub MyGrid1_SelectionChanged(sender As Object, e As System.EventArgs)    Handles MyGrid1.SelectionChanged
        Debug.Print(CStr(MyGrid1.MyRows(MyGrid1.CurrentRow.Index).Checked))
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles     Button1.Click
        For Each oRow As UIGridRow In MyGrid1.MySelectedRows
            oRow.Cells(1).Style.BackColor = Color.Red
        Next
    End Sub
End Class
于 2014-05-10T09:58:50.343 回答