2013-05-05 37 views
1

我有一個名爲UIGrid控制它從DataGridView
現在繼承,我想從DataGridViewRowCollection行物業類型更改爲UIGridRowCollection
我已經過載行屬性返回我喜歡的類型但它不工作,因爲我想 我需要,當我爲網格設置一個數據源,它添加了UIGridRow對象,而不是DataGridViewRow
我怎麼能做到這一點?
注:控制在45項目中的解決方案中使用,所以我不能改變的第三方解決方案繼承的DataGridView和改變行鍵入

這裏是一個示例代碼:

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屬性設置爲網格,行屬性仍計數0,如此看來,網格不使用重載的行物業

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屬性時,網格使用重載的行屬性,並添加行的類型UIGridRow而不是DataGridViewRow

+0

什麼建議嗎? – Mahdy 2013-05-07 11:28:58

回答

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