创建GridView自定义单元格

时间:2014-12-24 18:49:51

标签: c# .net winforms telerik radgridview

我有Winforms申请和Telerik GridView

我想要实现的是自定义创建自定义单元格并将PregressBar添加到Cell GridView中,所以我找到了this教程,解释了如何做到这一点,但因为我是一个非常新的开发人员,所以很难让我了解如何使用它来实现这一点。

我所做的就是创建这两个类:ProgressBarCellElementProgressBarColumn所以我问你是否可以帮助我。 我知道提出这类问题并不常见,但我做的都可以,但我仍然不知道如何使用它。

更新

这是我尝试过的(不工作):

private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
{
    if (e.CellType == typeof(GridDataCellElement))
    {
        GridViewDataColumn dataColumn = e.Column as GridViewDataColumn;
        if (dataColumn.UniqueName == "Progress")
        {
            e.CellType = typeof(ProgressBarColumn);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个例子。这是主要形式。

Public Partial Class Form1
Inherits Form

Private radGridView1 As RadGridView
Public Sub New()
    InitializeComponent()

    radGridView1 = New RadGridView()
    Me.Controls.Add(radGridView1)
    radGridView1.Dock = DockStyle.Fill

    Dim textCol As New GridViewTextBoxColumn("Name")
    radGridView1.Columns.Add(textCol)

    Dim ageCol As New GridViewDecimalColumn("Age")
    radGridView1.Columns.Add(ageCol)

    Dim prgsCol As New ProgressBarColumn("Progress")
    radGridView1.Columns.Add(prgsCol)

    radGridView1.Rows.Add("Steve", 21, 15)
    radGridView1.Rows.Add("John", 43, 90)

    radGridView1.Rows.Add("Mike", 23, 66)
End Sub
End Class

这是进度条单元格。

Class ProgressBarCellElement
Inherits GridDataCellElement
Private radProgressBarElement As RadProgressBarElement

Public Sub New(column As GridViewColumn, row As GridRowElement)
    MyBase.New(column, row)
End Sub

Protected Overrides Sub CreateChildElements()
    MyBase.CreateChildElements()

    radProgressBarElement = New RadProgressBarElement()
    Me.Children.Add(radProgressBarElement)
End Sub

Protected Overrides Sub SetContentCore(value As Object)
    If Me.Value IsNot Nothing AndAlso Me.Value <> DBNull.Value Then
        Me.radProgressBarElement.Value1 = Convert.ToInt32(Me.Value)
    End If
End Sub

Protected Overrides ReadOnly Property ThemeEffectiveType() As Type
    Get
        Return GetType(GridDataCellElement)
    End Get
End Property

Public Overrides Function IsCompatible(data As GridViewColumn, context As Object) As Boolean
    Return TypeOf data Is ProgressBarColumn AndAlso TypeOf context Is GridDataRowElement
End Function
End Class

这是进度条列。

Public Class ProgressBarColumn
Inherits GridViewDataColumn
Public Sub New(fieldName As String)
    MyBase.New(fieldName)
End Sub

Public Overrides Function GetCellType(row As GridViewRowInfo) As Type
    If TypeOf row Is GridViewDataRowInfo Then
        Return GetType(ProgressBarCellElement)
    End If
    Return MyBase.GetCellType(row)
End Function
End Class