继承DataGridView并更改行类型

时间:2013-05-05 13:19:54

标签: vb.net datagridview

我有一个名为UIGrid的控件,它继承自DataGridView
现在,我想将Rows属性类型从DataGridViewRowCollection更改为UIGridRowCollection
我重载了Rows属性以返回我的类型,但它不能正常工作 当我为网格设置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属性时,The 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

1 个答案:

答案 0 :(得分:0)

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

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