This is a migrated thread and some comments may be shown as answers.

Different cell type

9 Answers 326 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jiri
Top achievements
Rank 1
Jiri asked on 23 Jan 2014, 04:44 PM
Hi i need this add new column which by set by another column for example

id text value new column
1 test1 yes combobox
2 test2 no button

so for one row is new column radcombobox ane for second row radbutton.
thx for help.

9 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Jan 2014, 09:30 AM
Hello Jiri,

Thank you for contacting Telerik Support.

In order to achieve the desired behavior, it is necessary to create a custom column, which uses two custom cells. Here is a sample approach how to implement one column which uses CustomComboCell for each odd row and CustomCommandCell for each even cell. The editor for the CustomComboCell is a RadDropDownListEditor which is populated in the CellEditorInitialized.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        GridViewTextBoxColumn textBoxColumn = new GridViewTextBoxColumn("Text");
        radGridView1.Columns.Add(textBoxColumn);
 
        GridViewCheckBoxColumn checkBoxColumn = new GridViewCheckBoxColumn("Value");
        radGridView1.Columns.Add(checkBoxColumn);
 
        CustomColumn newColumn = new CustomColumn("NewColumn");
        radGridView1.Columns.Add(newColumn);
 
        for (int i = 0; i < 20; i++)
        {
            this.radGridView1.Rows.Add("Test" + i, i % 2 == 0, null);
        }
 
        this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
 
        this.radGridView1.CellBeginEdit += radGridView1_CellBeginEdit;
    }
 
    private void radGridView1_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        GridViewEditManager manager = sender as GridViewEditManager;
        if (manager.GridViewElement.GridControl.CurrentCell is CustomCommandCell)
        {
            e.Cancel = true;
        }
    }
 
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        GridViewEditManager manager = sender as GridViewEditManager;
        if (manager.GridViewElement.GridControl.CurrentCell is CustomComboCell)
        {
            RadDropDownListEditor editor = e.ActiveEditor as RadDropDownListEditor;
            RadDropDownListEditorElement element = editor.EditorElement as RadDropDownListEditorElement;
            element.DataSource = new List<string>() { "item1", "item2", "item3" };
        }
    }
}
 
public class CustomColumn : GridViewDataColumn
{
    public CustomColumn(string fieldName) : base(fieldName)
    {
    }
 
    public override Type GetCellType(GridViewRowInfo row)
    {
        if (row is GridViewDataRowInfo)
        {
            if (row.Index % 2 == 0)
            {
                return typeof(CustomComboCell);
            }
            else
            {
                return typeof(CustomCommandCell);
            }
        }
        return base.GetCellType(row);
    }
 
    public override Type GetDefaultEditorType()
    {
        return typeof(RadDropDownListEditor);
    }
}
 
public class CustomComboCell : GridDataCellElement
{
    public CustomComboCell(GridViewColumn column, GridRowElement row) : base(column, row)
    {
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridDataCellElement);
        }
    }
 
    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data is CustomColumn && context is GridDataRowElement;
    }
}
 
public class CustomCommandCell : GridDataCellElement
{
    private RadButtonElement button;
 
    public CustomCommandCell(GridViewColumn column, GridRowElement row) : base(column, row)
    {
        base.CreateChildElements();
        this.button = new RadButtonElement();
        this.button.Text = "Click me";
        this.button.Click += new EventHandler(Button_Click);
        this.Children.Add(button);
    }
 
    protected override Type ThemeEffectiveType
    {
        get
        {
            return typeof(GridDataCellElement);
        }
    }
 
    public override bool IsCompatible(GridViewColumn data, object context)
    {
        return data is CustomColumn && context is GridDataRowElement;
    }
 
    private void Button_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Clicked");
    }
}

You can find more information about implementing custom cells in our Creating custom cells help article.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Rawad
Top achievements
Rank 2
answered on 26 Jun 2014, 10:56 AM
Hi Desisalva

Could you please re-write your post in VB.net  and not in c#  in order to try it.

Thanks
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Jun 2014, 01:56 PM
Hello Rawad,

Thank you for writing.

Here is the code snippet in VB.NET:
Partial Public Class Form1
Inherits Form
    Public Sub New()
        InitializeComponent()
 
        Dim textBoxColumn As New GridViewTextBoxColumn("Text")
        RadGridView1.Columns.Add(textBoxColumn)
 
        Dim checkBoxColumn As New GridViewCheckBoxColumn("Value")
        RadGridView1.Columns.Add(checkBoxColumn)
 
        Dim newColumn As New CustomColumn("NewColumn")
        RadGridView1.Columns.Add(newColumn)
 
        Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
        For i As Integer = 0 To 19
            Me.RadGridView1.Rows.Add("Test" & i, i Mod 2 = 0, Nothing)
        Next
 
        AddHandler Me.RadGridView1.CellEditorInitialized, AddressOf radGridView1_CellEditorInitialized
 
        AddHandler Me.RadGridView1.CellBeginEdit, AddressOf radGridView1_CellBeginEdit
 
        AddHandler Me.RadGridView1.CurrentCellChanged, AddressOf radGridView1_CurrentCellChanged
    End Sub
 
    Private Sub radGridView1_CurrentCellChanged(sender As Object, e As CurrentCellChangedEventArgs)
        Dim grid As RadGridView = TryCast(sender, RadGridView)
        Dim current = grid.CurrentCell
    End Sub
 
    Private Sub radGridView1_CellBeginEdit(sender As Object, e As GridViewCellCancelEventArgs)
        Dim manager As GridViewEditManager = TryCast(sender, GridViewEditManager)
        If TypeOf manager.GridViewElement.GridControl.CurrentCell Is CustomCommandCell Then
            e.Cancel = True
        End If
    End Sub
 
    Private Sub radGridView1_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
        Dim manager As GridViewEditManager = TryCast(sender, GridViewEditManager)
        If TypeOf manager.GridViewElement.GridControl.CurrentCell Is CustomComboCell Then
            Dim editor As RadDropDownListEditor = TryCast(e.ActiveEditor, RadDropDownListEditor)
            Dim element As RadDropDownListEditorElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
            element.DataSource = New List(Of String)() From { _
                "item1", _
                "item2", _
                "item3" _
            }
        End If
    End Sub
End Class
 
Public Class CustomColumn
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
            If row.Index Mod 2 = 0 Then
                Return GetType(CustomComboCell)
            Else
                Return GetType(CustomCommandCell)
            End If
        End If
        Return MyBase.GetCellType(row)
    End Function
 
    Public Overrides Function GetDefaultEditorType() As Type
        Return GetType(RadDropDownListEditor)
    End Function
End Class
 
Public Class CustomComboCell
Inherits GridDataCellElement
    Public Sub New(column As GridViewColumn, row As GridRowElement)
        MyBase.New(column, row)
    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 CustomColumn AndAlso TypeOf context Is GridDataRowElement
    End Function
End Class
 
Public Class CustomCommandCell
Inherits GridDataCellElement
    Private button As RadButtonElement
 
    Public Sub New(column As GridViewColumn, row As GridRowElement)
        MyBase.New(column, row)
        MyBase.CreateChildElements()
        Me.button = New RadButtonElement()
        Me.button.Text = "Click me"
        AddHandler Me.button.Click, AddressOf Button_Click
        Me.Children.Add(button)
    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 CustomColumn AndAlso TypeOf context Is GridDataRowElement
    End Function
 
    Private Sub Button_Click(sender As Object, e As EventArgs)
        MessageBox.Show("Clicked")
    End Sub

Please feel free to use our free online converter.

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Rawad
Top achievements
Rank 2
answered on 01 Jul 2014, 05:31 AM
Dear Desislava

Thanks a lot.

It's well working with me, but how to make from the code above, the combobox not editable, just selective field, in another term user cannot write in this field, but just select an item from the list (item1, item2, item3)

Thanks
0
Rawad
Top achievements
Rank 2
answered on 01 Jul 2014, 11:28 AM
Dear  Desislava

Hope this post finds you well.
I tried to insert the following line in the cellEditorInitialized and it's working.

element.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList

I tried the code above, written in VB.net,  but I changed the Button_Click Events to display the value of the first cell, so I added the following lines :

  Me.button.Tag = row.RowInfo.Cells(0).Value  
  Me.button.Text = "Click me: " & row.RowInfo.Cells(0).Value      

in the class CustomCommandCell   Under the sub New.
and the sub  

    Private Sub Button_Click(sender As Object, e As EventArgs)
        MessageBox.Show(sender.Tag)
    End Sub
 
If the radgridview is big (no scroll bar are visible), the data are well displayed,   meanwhile if the radgridview is small (scroll bar is visible), the data is not well displayed.
please check the attached file for more details, there's a solution ?

Thanks

















0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 03 Jul 2014, 02:18 PM
Hello Rawad,

Thank you for writing back.
  • The DropDownStyle property determines if the text area at the top of the RadDropDownListEditor can be edited. A setting of DropDown (the default) allows editing and the DropDownList setting shows the text area as read-only.
  • Due to the UI virtualization in RadGridView, cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on.
    In order to prevent applying the formatting to other columns' cell elements (because of the cell reuse) all customization should be reset for the rest of the cell elements. Thus, the RadButtonElement.Text will not be displayed where is redundant:
    Private Sub Button_Click(sender As Object, e As EventArgs)
        Dim btn As RadButtonElement = TryCast(sender, RadButtonElement)
        Dim cell As GridDataCellElement = TryCast(btn.Parent, GridDataCellElement)
        If cell IsNot Nothing Then
            btn.Text = cell.RowInfo.Cells(0).Value
            cell.RowInfo.Tag = "clicked"
        End If
    End Sub
     
    Public Overrides Sub SetContent()
        MyBase.SetContent()
        If Me.button IsNot Nothing Then
     
            If Me.RowInfo.Tag = "clicked" Then
                Me.button.Text = Me.RowInfo.Cells(0).Value
            Else
                Me.button.Text = "Click me"
            End If
        End If
    End Sub
I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Bao
Top achievements
Rank 1
answered on 26 Jul 2017, 04:23 PM

Hi, 

How can i access GridDataCellElement from GridViewDataColumn.

I have a case, i have a gridview with 3 custom checkbox column. after binding completely, when i click button, i want to set check or uncheck custom checkbox column by loop the gridview rows. But i can not access to the cell element by using loop.

 

0
Bao
Top achievements
Rank 1
answered on 26 Jul 2017, 04:25 PM

and because my gridview is self hierarchy when i use it:

gridview.TableElement.GetCellElement in loop rows. it only gets the parent row, about the child row is collapsed. it can not access.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Jul 2017, 09:17 AM
Hello Bao, 

Thank you for writing.  

Your question has already been answered in this thread. Please, see our answer there for more information.
We kindly ask you to use just one thread for a specific problem to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

Thank you for your understanding.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Jiri
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Rawad
Top achievements
Rank 2
Bao
Top achievements
Rank 1
Share this question
or