ComponentOne True DBGrid创建自定义单元格

时间:2014-12-19 19:24:08

标签: vb.net datatable custom-cell componentone dbgrid

首先感谢你的帮助。

这是关于Componentone的True DBGrid,所以这可能不是期待答案的最佳位置,但在这一点上,我觉得我已尽可能多地完成,所以我&#39我愿意试一试。

过去几天我花了很长时间试图弄清楚如何在True DBGrid中创建自定义单元格,而且目前我已经卡住了。我已经浏览了尽可能多的文档,而真正的DBGrid的所有教程,无论我能得到的最多,都会在下面的附图中显示。

Example of One Custom Column Displayed

如果您双击一个单元格,我可以在他们点击的单元格中显示自定义控件。但是,我希望单元格始终可见,而不仅仅是在单击时,我希望它基于行本身而不仅仅是displayColumn,就像我现在正在做的那样我重新加载& #34; DisplayColumn.DataColumn.Editor"每次点击不同的单元格。我的性能代码如下所示。

' Retrieve Column
Dim objPrefDisplayColumn As C1.Win.C1TrueDBGrid.C1DisplayColumn = Me.TestGrid.Splits(0).DisplayColumns("ColumnFieldName")
'Retrieve data from database
Dim data As DataTable = ' My call to database table goes here Retrieve data that relates to row
' Create Custom Controller and Insert Data from table into it
Dim prvNewCellList As New TestCellList
prvNewCellList.LabelButtonHeight = Me.TestGrid.RowHeight / pref.Rows.Count
prvNewCellList.LabelWidth = (objPrefDisplayColumn.Width * 0.9)
prvNewCellList.ButtonWidth = (objPrefDisplayColumn.Width * 0.1)
 prvNewCellList.Action_LoadUI(data)
' Assign Custom Controller to Column
objPrefDisplayColumn.DataColumn.Editor = prvNewCellList
objPrefDisplayColumn.Button = True
objPrefDisplayColumn.ButtonAlways = True
objPrefDisplayColumn.DropDownList = False
objPrefDisplayColumn.DataColumn.DropDown = Nothing

我知道当我查看DataGridView的教程时,这应该是可能的,就像下面的链接一样,它们放置在自定义" DataGridViewTextBoxCell"中,就像图像所示。

Turtorial for Custom DataGridViewCells

根据我读到的关于TrueDBGrid的内容,以及ComponentOne最有可能使用DataGridView作为创建TrueDBGrid的模板的预期逻辑,我希望创建自定义单元格的方式非常相似。但是,在尝试使用如下所示的TrueDBGrid重新创建此示例后,我发现列不接受" DataGridViewColumn"当我尝试将其更改为" C1DataColumn"为了满足它的期望,我发现这个课程没有类似的领域" CellTemplate"可用于创建自定义单元格。在这一点上,我几乎已经准备好相信在开发TrueDBGrid期间忘记了定制单元的功能,但是,我更愿意被证明是错误的。

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim col As New DataGridViewRolloverCellColumn()
            Me.TestGrid.Columns.Add(col)
        Catch ex As Exception
            MsgBox("error arrose", vbInformation, "error")
        End Try
    End Sub
End Class

Public Class DataGridViewRolloverCellColumn
    Inherits DataGridViewColumn

    Public Sub New()
        Me.CellTemplate = New DataGridViewRolloverCell()
    End Sub

End Class

Public Class DataGridViewRolloverCell
    Inherits DataGridViewTextBoxCell

    Protected Overrides Sub Paint( _
    ByVal graphics As Graphics, _
    ByVal clipBounds As Rectangle, _
    ByVal cellBounds As Rectangle, _
    ByVal rowIndex As Integer, _
    ByVal elementState As DataGridViewElementStates, _
    ByVal value As Object, _
    ByVal formattedValue As Object, _
    ByVal errorText As String, _
    ByVal cellStyle As DataGridViewCellStyle, _
    ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, _
    ByVal paintParts As DataGridViewPaintParts)

        ' Call the base class method to paint the default cell appearance.
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, _
            value, formattedValue, errorText, cellStyle, _
            advancedBorderStyle, paintParts)

        ' Retrieve the client location of the mouse pointer.
        Dim cursorPosition As Point = _
            Me.DataGridView.PointToClient(Cursor.Position)

        ' If the mouse pointer is over the current cell, draw a custom border.
        If cellBounds.Contains(cursorPosition) Then
            Dim newRect As New Rectangle(cellBounds.X + 1, _
                cellBounds.Y + 1, cellBounds.Width - 4, _
                cellBounds.Height - 4)
            graphics.DrawRectangle(Pens.Red, newRect)
        End If
    End Sub
End Class

再次感谢您的帮助

1 个答案:

答案 0 :(得分:0)

过了一段时间我找到了解决方法。

以下解决方案对单元格进行了一个共同的控制,并给出了它存在的外观,而实际上它只是存在于单元格之上。要执行此解决方案,您需要将公共控件添加到网格的控制器,并在每次调用paint事件时更新公共控件位置和可见性。

Private Sub LoadGrid()
    ' Create and Load Common control
    Dim prvsdServiceDelivery As ServiceDelTable = New ServiceDelTable()
    prvsdServiceDelivery.Action_LoadUI(DataTable)
    ' Add Common Control to Datastruct so it can be accessed anytime
    Dictionary.put(key, prvsdServiceDelivery)
    ' insert hosted control into grid
    C1TrueDBGrid.Controls.Add(prvptPreferences)
End Sub

Private Sub TestGrid_Paint(sender As Object, e As PaintEventArgs) Handles TestGrid.Paint

    For Each item As KeyValuePair(Of String, prvsdServiceDelivery) In Dictionary
        Try
            Dim prvsdServiceDelivery as ServiceDelTable  = item.Value

            ' get cell rect, may throw error if cell is outside the bounds of table's rectangle
            Dim bnds As Rectangle = frc1tdOwnerTable.Splits(0).GetCellBounds(row,C1TrueDBGrid.Splits(0).DisplayColumns.IndexOf(column))

            ' Set Visibility and move if error isn't thrown
            prvsdServiceDelivery.Bounds = bnds
            prvsdServiceDelivery.Visible = True
        Catch ex As Exception
            ' Set as invisible if exception thrown to say not found
            prvsdServiceDelivery.Visible = False
        End Try
    Next
End Sub