Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Dynamically adding controls in the columns in the grid

Not answered Dynamically adding controls in the columns in the grid

Feed from this thread
  • VEDA VIDVA NARAYANAN SATTANATHAN avatar

    Posted on Dec 17, 2008 (permalink)

    Hi,
    I am currently trying to create controls inside a column of the grid view dynamically. My requirement is like i want to create textbox or combo box and bind it into the column depending on the condition. Is it possible to have textbox and combobox in the same column?

    Can you please enlighten us with some sample code?

    Thanks in advance,
    Veda

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Dec 19, 2008 (permalink)

    Hi VEDA VIDVA NARAYANAN SATTANATHAN,

    Thank you for the question.

    You could customize every cell in RadGridView through the CellFormating and ViewCellFormating events.

    To add RadComboBoxElement in data cells you can use CellFormating. Please, review the code-block below as example:
     
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
    {  
        if (e.CellElement.ColumnIndex == 0 &&  
            (float)this.radGridView1.Rows[e.CellElement.RowIndex].Cells["Discount"].Value == 0)  
        {  
            e.CellElement.Children.Add(new RadComboBoxElement());  
        }  

    You can find more information on these in the provided documentation.

    Do not hesitate to contact me again if you have other questions.

    Best wishes,
    Martin Vasilev
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.

    Reply

  • Mike avatar

    Posted on Feb 13, 2009 (permalink)

    I've added a RadComboBoxElement to a  Parent GridViewTextBoxColumn.  I want to populate with values from a custom object.
    How do you add items to the RadComboBoxElement?

     

     

     

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Feb 16, 2009 (permalink)

    Hi Mike,

    Thank you writing.

    You can add items to the RadComboBoxEditor in the EditorRequiered event. Please, review the code block below as example:
     
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)  
    {  
        RadComboBoxEditor comboEditor = e.Editor as RadComboBoxEditor;  
     
        if (comboEditor != null)  
        {  
            comboEditor.Items.Clear();  
     
            for (int i = 0; i < (int)this.radGridView1.CurrentRow.Cells[2].Value; i++)  
            {  
                comboEditor.Items.Add(new RadComboBoxItem(String.Format("item{0}", i+1)));  
            }  
     
            comboEditor.SelectedIndex = 0;            
        }  

    Hope this helps. Write me back if you have other questions.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Geert avatar

    Posted on Feb 26, 2009 (permalink)

    This gives a problem in my code:

    this is my code to set in the first row comboboxes
    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
            {  
                if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridTableHeaderRowElement))  
                {  
                    if (e.CellElement.RowIndex == 0)  
                    {  
                        if (e.CellElement.Children.Count > 0)  
                            return;  
     
                        RadComboBoxEditor element = new RadComboBoxEditor();  
                        element.Items.Add(new RadComboBoxItem("test"));  
                        element.Items.Add(new RadComboBoxItem("test"));  
                        element.Items.Add(new RadComboBoxItem("test"));  
                        element.Items.Add(new RadComboBoxItem("test"));  
                        element.Items.Add(new RadComboBoxItem("test"));  
                        e.CellElement.Children.Add(element);  
                        // apply theme to the progress bar    
                        ApplyThemeToElement(element, "ControlDefault");  
     
                    }  
                }  
     
            }  
            private void ApplyThemeToElement(RadItem item, string themeName)  
            {  
                DefaultStyleBuilder builder =  
                        ThemeResolutionService.GetStyleSheetBuilder(item, themeName) as DefaultStyleBuilder;  
                if (builder != null)  
                    //clone because control might modify it later  
                    item.Style = new XmlStyleSheet(builder.Style).GetStyleSheet();  
            } 
    I've tried to put some items in the combobox, but when I click it open, it is empty.

    So I added this code:
    private void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)  
            {  
                RadComboBoxEditor comboEditor = e.Editor as RadComboBoxEditor;  
                string t = e.EditorType.ToString();  
                  
     
                if (comboEditor != null)  
                {  
                    comboEditor.Items.Clear();  
     
                    for (int i = 0; i < (int)this.radGridView1.CurrentRow.Cells[2].Value; i++)  
                    {  
                        comboEditor.Items.Add(new RadComboBoxItem(String.Format("item{0}", i + 1)));  
                    }  
     
                    comboEditor.SelectedIndex = 0;  
                }     
     
            }  
     
    The problem is that the e.Editor type is a textbox and not a combobox. It's like the textbox is on top of the combobox...

    Reply

  • Geert avatar

    Posted on Feb 27, 2009 (permalink)

    I'm still having the problem. the strangest thing is when I right click on the combobox. The list is being opened. So that works fine. But when I use the left click. The focus goed to the textbox at the back of the combobox.

    When I type I can see that the focus is on the textbox. Is there a way to solve this?

    I've allready tried 

     

    e.CellElement.CanFocus =

    false;

    but with no success...

     

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Feb 27, 2009 (permalink)

    Hi Geert,

    Thank you for contacting me again.

    After a couple of tests I have to admit that there is not an easy way to create different cells for one column. The main difficulty comes from the fact that you have to implement a custom editor, and change it  depending on the cell type. The standard editor for GridViewDataColumn is a text editor, and the EditorRequired event does not work there. Unfortunately, implementing custom editor is a very hard task and I cannot recommend it. We will consider adding such of functionality in some of the future releases.

    Write me back if you have other questions.
     

    Kind regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Dipak avatar

    Posted on Jul 9, 2009 (permalink)

    Did you addressed this issue in  version 2009.2 701 ?

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Jul 15, 2009 (permalink)

    Hi Dipak,

    Thank you for the question. Actually, with the new version Q2 2009, I have managed to implement different type of cell elements in one column. I have prepared a simple scenario that changes grid's cells to ComboBoxes for every even row in a custom GridViewTextBoxColumn. Please, find the example project as attachment to this message.

    Write me back if you have any other questions.

    All the best,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Dipak avatar

    Posted on Jul 20, 2009 (permalink)

    Thanks Martin.

    This code is very usefull for me. I am looking for the sample and help I could not find anything related to date mask in the grid. Could you please update this example so that user can enter the formated Date (MM/dd/yy) in the masked edit kind of editor along with the lookup and plain text.

    Thanks
    Dipak



    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Jul 23, 2009 (permalink)

    Hello Dipak,

    Thank you for getting back to me. It occurred to me a way to implement a similar scenario. Since in the latest version changing the editor for chosen cell become very easy, you can just change it to RadDateTimePickerEditor for the selected cells. You also could make a custom editor that matches exactly your needs. Please, consider the following code:
     
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)  
    {  
        if (this.radGridView1.Columns[2].IsCurrent && (int)this.radGridView1.CurrentRow.Cells[0].Value % 2 == 0)  
        {  
            e.EditorType = typeof(RadDateTimeEditor);  
        }  

    Write me back if you need additional assistance.

    Regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Dipak avatar

    Posted on Jul 24, 2009 (permalink)

    Hi

    Thanks this works fine for me. Now I can edit DateValue in DateTimeEditor. I have one formating problem. I am showing the Date in the grid in MM/dd/yyyy format. Once I edit this date and close the editor it will update the Date along with the time. I dont want the time portion here.Same thing happens when I am adding new date in this it is also showing Date and Time.

    Could you please help me to format when it transfers from editor to grid cell ?
    Thanks
    Dipak


    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Jul 29, 2009 (permalink)

    Hello Dipak,

    Thank you for writing again. RadGridView does not support different formatting for the cells in one and the same column. Nevertheless, you can work-around this by changing cell element text in the CellFormattingEvent. Please, consider the following code:
     
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
    {  
        if (e.CellElement.RowInfo is GridViewDataRowInfo && e.CellElement.ColumnIndex == 2)  
        {  
            DateTime date;  
            if (DateTime.TryParse(e.CellElement.RowInfo.Cells[2].Value.ToString(), out date))  
            {  
                e.CellElement.Text = date.ToString("d MMMM yyyy");  
            }  
        }  

    Best wishes,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Peter Bogoyavlensky avatar

    Posted on Oct 21, 2009 (permalink)

    I have similiar problem - in one column I want have ComboBox cells and DateTime cells (depending on some conditions - actually it must be choosed by combobox value in other column).

    As I understand - it can be done by catching CellFormatting event and proceed by e.CellElement.Children collection, add here a RadComboBoxElement or RadDateTimeEditorElement (is it?).

    Questions:
    1. What kind of base column type must I set?
    2. Must I previously remove element from e.CellElement.Children collection (in order change cell type from combobox to datetime, for example)?
    3. How often CellFormatting event is firing? Can I be sure that changing combobox value in other "ruler" column will start this event?

    I  am not very familiar with C#, would it be possible obtain some examples in VB.Net?

    I use Telerik RadControls for WinForms v2009.2.729.

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Oct 26, 2009 (permalink)

    Hello Peter Bogoyavlensky,

    Thank you for writing.

    You are right, you can dynamically change the cell elements in CellFormatting event through CellElement.Children collection.

    1. You can use any column type depending on your scenario. For instance, when I tested it, I used GridViewTextBoxColumn.

    2. Since you want to swap elements between the ComboBox and the DateTimePicker, you have to clear the Children collection before adding the appropriate element.

    3. CellFormatting event fires on every state change e.g. scrolling, filtering, sorting, etc. In most cases, it is enough to handle every situation including the described.

    Please, consider the following code-block as an example for a similar scenario:

    Private Sub radGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As GridViewCellCancelEventArgs)
        If e.ColumnIndex = 2 Then
            e.Cancel = True
        End If
    End Sub
     
    Private Sub radGridView1_CellFormatting(ByVal sender As Object, ByVal e As CellFormattingEventArgs)
        If TypeOf e.CellElement.RowInfo Is GridViewDataRowInfo Then
            Dim conditionValue As Object = e.CellElement.RowInfo.Cells("id").Value
            Dim currentValue As Object = DirectCast(e.CellElement, GridDataCellElement).Value
            If e.CellElement.ColumnIndex = 2 Then
                If CInt(conditionValue) Mod 4 = 0 Then
                    Dim datePicker As RadDateTimeEditorElement = Nothing
                    If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadDateTimeEditorElement) Then
                        e.CellElement.Children.Clear()
                        datePicker = New RadDateTimeEditorElement()
                        AddHandler datePicker.ValueChanged, AddressOf datePicker_ValueChanged
                        e.CellElement.Children.Add(datePicker)
                    ElseIf TypeOf e.CellElement.Children(0) Is RadDateTimeEditorElement Then
                        datePicker = DirectCast(e.CellElement.Children(0), RadDateTimeEditorElement)
                    End If
                    Dim d As DateTime
                    If datePicker IsNot Nothing AndAlso currentValue IsNot Nothing AndAlso DateTime.TryParse(currentValue.ToString(), d) Then
                        Me.settingValue = True
                        datePicker.Value = Convert.ToDateTime(currentValue)
                        Me.settingValue = False
                    End If
                Else
                    Dim combo As RadComboBoxElement = Nothing
                    If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadComboBoxElement) Then
                        e.CellElement.Children.Clear()
                        combo = New RadComboBoxElement()
                        combo.ForeColor = Color.Black
                        AddHandler combo.SelectedIndexChanged, AddressOf combo_SelectedIndexChanged
                        For i As Integer = 0 To 4
                            combo.Items.Add(New RadComboBoxItem([String].Format("Value{0}", i + 1)))
                        Next
                        e.CellElement.Children.Add(combo)
                    ElseIf TypeOf e.CellElement.Children(0) Is RadComboBoxElement Then
                        combo = DirectCast(e.CellElement.Children(0), RadComboBoxElement)
                    End If
                   
                    If combo IsNot Nothing Then
                        Me.settingValue = True
                        If currentValue IsNot Nothing AndAlso currentValue <> DBNull.Value Then
                            combo.SelectedIndex = Convert.ToInt32(currentValue)
                        Else
                            combo.SelectedIndex = -1
                        End If
                        Me.settingValue = False
                    End If
                End If
            ElseIf e.CellElement.Children.Count > 0 AndAlso TypeOf e.CellElement.Children(0) Is RadComboBoxElement Then
                e.CellElement.Children.Clear()
            End If
        End If
    End Sub
     
    Private Sub datePicker_ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.settingValue AndAlso Me.radGridView1.CurrentCell IsNot Nothing Then
            Dim datePicker As RadDateTimeEditorElement = DirectCast(sender, RadDateTimeEditorElement)
            Me.radGridView1.CurrentCell.Value = datePicker.Value
        End If
    End Sub
     
    Private Sub combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.settingValue AndAlso Me.radGridView1.CurrentCell IsNot Nothing Then
            Dim combo As RadComboBoxElement = DirectCast(sender, RadComboBoxElement)
            Me.radGridView1.CurrentCell.Value = combo.SelectedIndex
        End If
    End Sub

    Do not hesitate to contact me again if you have any other questions.

    Greetings,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

    Reply

  • Posted on Jan 1, 2010 (permalink)

    Can you explain in a bit more detail specifically what's going on in the CellBeginEdit event? I think I understand it, but maybe not.

    Are you canceling going into edit mode for that column and in those rows because you have replaced the textbox with a control that is different than other types of edit controls in normally appear in that column? The code sample wasn't commented and I have a similar situation where I'm replacing the textbox with a combobox that is different in the last column and based on the information entered in the previous column. Thanks for any insight!

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Jan 7, 2010 (permalink)

    Hi mtaber,

    Thank you for the question.

    I have canceled the BeginEdit event because I have substituted cell elements with my own elements (RadDateTimeEditorElement or RadComboBoxElement). In this way, I do not need to enter in edit mode and initialize the default column editor, because it is going to mess up with the new elements. I just use the elements' events to manage the change of the value and to save the cell value.

    I hope this makes the things clearer. Write me back if you need additonal assistance.

    Kind regards,
    Martin Vasilev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.

    Reply

  • Posted on Mar 8, 2010 (permalink)

    Hello Support,

    We need to have one column to show different editor type when editing according the values in other cells.

    1. if the values in other cells match condition A, the editor type should be combobox when editing.
    2. otherwise the editor type should be a datetimepicker.

    Could you give a more elaborate sample in doing this?

    BR/shortie

    Reply

  • Martin Vasilev Martin Vasilev admin's avatar

    Posted on Mar 11, 2010 (permalink)

    Hi ChunChang,

    Thank you for the question. You can use EditorRequierd event to change the cell editor based on a condition. Please, refer to our documentation for a detailed description on how to do that. Let me know if you have any additional questions.

    Greetings,
    Martin Vasilev
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.

    Reply

  • Peter Bogoyavlensky avatar

    Posted on Feb 3, 2012 (permalink)

    I used version 2010.1.10.308 before and made some example:

    There is grid with two column (type of GridViewComboBoxColumn). First column has DataSource like "Combo1", "Combo2" and "Date". First and second values must populate second cell with different Datasource, and third value must populate second cell with DateTime value.

    Here is example code (I presume that it's not so smart code, sorry for that):

    Public Class RadForm1
        Dim Condit As System.Collections.ArrayList
        Dim CbValue1 As System.Collections.ArrayList
        Dim CbValue2 As System.Collections.ArrayList
     
        Private Sub RadForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Condit = New System.Collections.ArrayList
            Condit.Add("Combo1")
            Condit.Add("Combo2")
            Condit.Add("Date")
            CbValue1 = New System.Collections.ArrayList
            CbValue1.Add("C1_1")
            CbValue1.Add("C1_2")
            CbValue2 = New System.Collections.ArrayList
            CbValue2.Add("C2_1")
            CbValue2.Add("C2_2")
     
            Dim column As Telerik.WinControls.UI.GridViewComboBoxColumn
            column = gridLogic.Columns(0)
            column.DataType = GetType(String)
            column.DataSource = Condit
     
            column = gridLogic.Columns(1)
            column.DataType = GetType(String)
            column.DataSource = CbValue1
     
            gridLogic.Rows.AddNew()
            gridLogic.Rows(0).Cells(0).Value = "Combo1"
            gridLogic.Rows(0).Cells(1).Value = "C1_1"
     
            gridLogic.Rows.AddNew()
            gridLogic.Rows(1).Cells(0).Value = "Combo1"
            gridLogic.Rows(1).Cells(1).Value = "C1_2"
     
            gridLogic.Rows.AddNew()
            gridLogic.Rows(2).Cells(0).Value = "Combo1"
            gridLogic.Rows(2).Cells(1).Value = "C1_1"
        End Sub
     
     
        Private Sub gridLogic_CellBeginEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles gridLogic.CellBeginEdit
            Dim NumRow As Integer = gridLogic.Rows.IndexOf(gridLogic.CurrentRow)
            If gridLogic.Columns(1).IsCurrent AndAlso gridLogic.Rows(NumRow).Cells(0).Value = "Date" Then
                If gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Count = 0 Then
                    Dim dtel As New Telerik.WinControls.UI.RadDateTimeEditorElement
                    AddHandler dtel.ValueChanged, AddressOf gridlogic_DateTime_ValueChanged
                    dtel.Tag = NumRow
                    dtel.Value = Now
                    gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Add(dtel)
                    e.Cancel = True
                Else
                    e.Cancel = True
                End If
            End If
     
            If gridLogic.Columns(0).IsCurrent Then
                Dim editor As Telerik.WinControls.UI.IInputEditor = Me.gridLogic.ActiveEditor
                If TypeOf (editor) Is Telerik.WinControls.UI.RadDropDownListEditor Then
                    Dim comboElement As Telerik.WinControls.UI.RadDropDownListEditorElement = CType(CType(editor, Telerik.WinControls.UI.RadDropDownListEditor).EditorElement, Telerik.WinControls.UI.RadDropDownListEditorElement)
                    AddHandler (comboElement.SelectedIndexChanged), AddressOf gridlogic_Condition_SelectedIndexChanged
                End If
            End If
        End Sub
     
        Private Sub gridlogic_Condition_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As EventArgs)
            Dim NumRow As Integer = gridLogic.Rows.IndexOf(gridLogic.CurrentRow)
            Dim NumCol As Integer = gridLogic.Columns.IndexOf(gridLogic.CurrentColumn)
     
            If CType(sender, Telerik.WinControls.UI.RadDropDownListEditorElement).SelectedIndex < 0 Then Exit Sub
            Dim item As String = CType(sender, Telerik.WinControls.UI.RadDropDownListEditorElement).SelectedItem.Value
     
            If item = "Combo1" Then
                If gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Count > 0 Then
                    If Not TypeOf gridLogic.Rows(NumRow).Cells(1).CellElement.Children(0) Is Telerik.WinControls.UI.RadDropDownListEditorElement Then
                        gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Remove(gridLogic.Rows(NumRow).Cells(1).CellElement.Children(0))
                    End If
                End If
                gridLogic.Rows(NumRow).Cells(1).Value = "C1_1"
            End If
     
            If item = "Combo2" Then
                If gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Count > 0 Then
                    If Not TypeOf gridLogic.Rows(NumRow).Cells(1).CellElement.Children(0) Is Telerik.WinControls.UI.RadDropDownListEditorElement Then
                        gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Remove(gridLogic.Rows(NumRow).Cells(1).CellElement.Children(0))
                    End If
                End If
                gridLogic.Rows(NumRow).Cells(1).Value = "C2_1"
            End If
     
            If item = "Date" Then
                If gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Count = 0 Then
                    Dim dtel As New Telerik.WinControls.UI.RadDateTimeEditorElement
                    AddHandler dtel.ValueChanged, AddressOf gridlogic_DateTime_ValueChanged
                    dtel.Tag = NumRow
                    Try
                        dtel.Value = Now
                    Catch ex As Exception
                    End Try
                    gridLogic.Rows(NumRow).Cells(1).CellElement.Children.Add(dtel)
                End If
            End If
        End Sub
     
        Private Sub gridlogic_DateTime_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim dt As Telerik.WinControls.UI.RadDateTimeEditorElement = CType(sender, Telerik.WinControls.UI.RadDateTimeEditorElement)
            gridLogic.Rows(dt.Tag).Cells(1).Value = dt.Value
        End Sub
     
        Private Sub gridLogic_CellEndEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gridLogic.CellEndEdit
            If gridLogic.Columns(0).IsCurrent Then
                Dim editor As Telerik.WinControls.UI.IInputEditor = Me.gridLogic.ActiveEditor
                If TypeOf (editor) Is Telerik.WinControls.UI.RadDropDownListEditor Then
                    Dim comboElement As Telerik.WinControls.UI.RadDropDownListEditorElement = CType(CType(editor, Telerik.WinControls.UI.RadDropDownListEditor).EditorElement, Telerik.WinControls.UI.RadDropDownListEditorElement)
                    RemoveHandler (comboElement.SelectedIndexChanged), AddressOf gridlogic_Condition_SelectedIndexChanged
                End If
            End If
        End Sub
     
        Private Sub gridLogic_CellEditorInitialized(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles gridLogic.CellEditorInitialized
            If gridLogic.Columns(1).IsCurrent Then
                If gridLogic.CurrentRow.Cells(0).Value = "Combo1" Or gridLogic.CurrentRow.Cells(0).Value = "Combo2" Then
                    Dim NumRow As Integer = gridLogic.Rows.IndexOf(gridLogic.CurrentRow)
                    Dim editor As Telerik.WinControls.UI.RadDropDownListEditor = gridLogic.ActiveEditor
                    Dim editorElement As Telerik.WinControls.UI.RadDropDownListEditorElement = editor.EditorElement
     
                    If gridLogic.CurrentRow.Cells(0).Value = "Combo1" Then
                        editorElement.DataSource = CbValue1
                        editorElement.SelectedText = "C1_1"
                    End If
                    If gridLogic.CurrentRow.Cells(0).Value = "Combo2" Then
                        editorElement.DataSource = CbValue2
                        editorElement.SelectedText = "C2_1"
                    End If
                End If
            End If
        End Sub
     
    End Class

    It works fine but I upgrade to latest version 2011.3.11.1219.

    I've changed RadComboBoxEditor to RadDropDownListEditor, and RadComboBoxEditorElement to RadDropDownListEditorElement. But I can not obtain CellElement from specific cell to change editor (from DropDownList to DateTimeEditor, or remove DateTimeEditor).

    How can I change code? Is here some simplier way to obtain result (described above)?

    Reply

  • Svett Svett admin's avatar

    Posted on Feb 7, 2012 (permalink)

    Hello Peter,

    It is not recommend to use the cell element outside the CellFormatting event because of the UI virtualization of RadGridView. I recommend using the EditorRequired event where you can replace the default editor with the one that you want. I am enclosing a sample project which demonstrates how you can do it.

    Greetings,
    Svett
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
    Attached files

    Reply

  • Peter Bogoyavlensky avatar

    Posted on Feb 13, 2012 (permalink)

    Sure, catching EditorRequired event is helpful to change curent editor to desired (I mean, in accordance with first column value).
    But this event fires only if I click on that cell - here I can change editor, populate Datasource property for combobox, set cell's value, etc.

    But how can I change this editor before clicking on that cell? I mean, I change value in first column from "Combo" to "Date" and want to reflect that change immediatelly - value in second column must be set to appropriate (some date, probably stored in my value's array). Otherwise, if I change value in first column from "Date" to "Combo" - value in second column must be set to appropriate (some value from list in accordance with Datasource array linked with that combobox).

    If I change value in second column's cell by catching CellValueChanged event from first column's cell - it can produce error because type of second column's cell would not be affordable for that new value.

    I presume that I miss something.

    Reply

  • Svett Svett admin's avatar

    Posted on Feb 15, 2012 (permalink)

    Hello Peter,

    First of all, if you are changing the editor dynamically, the underlying data source of the RadGridView should support the data types for all editors of the column that may appear. That means, if you have spin editor and date time picker, the underline field should be of the type that can host both values. I recommend using an object type for the field where you have dynamic editors.

    By following your illustration, I concluded that you have two active editors which is impossible. I am not able to assist you efficiently by following the given information. Could you please open a support ticket where you can enclose a project which demonstrates your approach? In addition, give us the exact steps that we should follow to reproduce the error that you have experienced. This will allow us to assist you further.

    Greetings,
    Svett
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Dynamically adding controls in the columns in the grid
Related resources for "Dynamically adding controls in the columns in the grid"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]