【问题标题】:Inheriting DataGridView and changing rows type继承 DataGridView 并更改行类型
【发布时间】:2013-05-05 13:19:53
【问题描述】:

我有一个名为 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属性时,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

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-04
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多