Dynamically adding controls in the columns in the grid

32 posts, 0 answers
  1. VEDA VIDVA NARAYANAN SATTANATHAN
    VEDA VIDVA NARAYANAN SATTANATHAN avatar
    18 posts
    Member since:
    Aug 2008

    Posted 17 Dec 2008 Link to this post

    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
  2. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 19 Dec 2008 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Mike
    Mike avatar
    1 posts
    Member since:
    May 2008

    Posted 13 Feb 2009 Link to this post

    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?

     

     

     

  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 16 Feb 2009 Link to this post

    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.
  6. Geert
    Geert avatar
    14 posts
    Member since:
    Feb 2009

    Posted 26 Feb 2009 Link to this post

    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...
  7. Geert
    Geert avatar
    14 posts
    Member since:
    Feb 2009

    Posted 27 Feb 2009 Link to this post

    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...

     

  8. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 27 Feb 2009 Link to this post

    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.
  9. Dipak
    Dipak avatar
    19 posts
    Member since:
    Oct 2008

    Posted 09 Jul 2009 Link to this post

    Did you addressed this issue in  version 2009.2 701 ?
  10. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 15 Jul 2009 Link to this post

    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.
  11. Dipak
    Dipak avatar
    19 posts
    Member since:
    Oct 2008

    Posted 20 Jul 2009 Link to this post

    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



  12. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 23 Jul 2009 Link to this post

    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.
  13. Dipak
    Dipak avatar
    19 posts
    Member since:
    Oct 2008

    Posted 24 Jul 2009 Link to this post

    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


  14. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 29 Jul 2009 Link to this post

    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.
  15. Peter Bogoyavlensky
    Peter Bogoyavlensky avatar
    20 posts
    Member since:
    Feb 2009

    Posted 21 Oct 2009 Link to this post

    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.
  16. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 26 Oct 2009 Link to this post

    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.
  17. mtaber
    mtaber avatar
    12 posts
    Member since:
    Oct 2006

    Posted 01 Jan 2010 Link to this post

    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!
  18. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 07 Jan 2010 Link to this post

    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.
  19. shortie
    shortie avatar
    23 posts
    Member since:
    Jul 2008

    Posted 08 Mar 2010 Link to this post

    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
  20. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 11 Mar 2010 Link to this post

    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.
  21. Peter Bogoyavlensky
    Peter Bogoyavlensky avatar
    20 posts
    Member since:
    Feb 2009

    Posted 03 Feb 2012 Link to this post

    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)?
  22. Svett
    Admin
    Svett avatar
    728 posts

    Posted 07 Feb 2012 Link to this post

    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 >>
  23. Peter Bogoyavlensky
    Peter Bogoyavlensky avatar
    20 posts
    Member since:
    Feb 2009

    Posted 13 Feb 2012 Link to this post

    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.
  24. Svett
    Admin
    Svett avatar
    728 posts

    Posted 15 Feb 2012 Link to this post

    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 >>
  25. Erin Arroyo
    Erin Arroyo avatar
    4 posts
    Member since:
    Feb 2010

    Posted 10 Mar 2014 Link to this post

    Hi,
    I have a grid that I have bound my columns to a datareader.  I have 2 columns that I look at in this grid when wanting to make a custom combobox for a cell and they are "Batch Status" and "Status ID" and they are both textbox cell types.  For the column "Batch Status" I want to make it a combobox for certain cells if the "Batch Status" is "Open".

    To start out I create a list array of all my combobox items.
    Private mstrBatchStatusList As New List(Of ComboBoxDataSourceObject)()

    Then I go ahead and use CellFormatting to make the cell a combobox if the Batch Status is "Open".
    Private Sub rgvBatch_CellBeginEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellCancelEventArgs) Handles rgvBatch.CellBeginEdit
            '
            ' Got this from a website.  http://www.telerik.com/forums/dynamically-adding-controls-in-the-columns-in-the-grid
            ' The status column is a canceled Edit because I have substituted cell elements with my own elements
            ' (RadDropDownListEditorElement). 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.
            '
            If e.Column.HeaderText = "Status" Then
                e.Cancel = True
            End If
        End Sub
        Private Sub rgvBatch_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles rgvBatch.CellFormatting
            '
            ' Edit
            '
            If e.CellElement.ColumnInfo.HeaderText = "Edit" Then
                'This is how we get the RadButtonElement instance from the cell
                Dim button As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
                If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                    Dim title As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                    If title = "Deleted" Then
                        button.Enabled = False
                    Else
                        button.Enabled = True
                    End If
                End If
            End If
            '
            ' Delete
            '
            If e.CellElement.ColumnInfo.HeaderText = "Delete" Then
                'This is how we get the RadButtonElement instance from the cell
                Dim button As RadButtonElement = CType(e.CellElement.Children(0), RadButtonElement)
                If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                    Dim title As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                    If title <> "Open" Or mblnReadOnly Then
                        button.Enabled = False
                    Else
                        button.Enabled = True
                    End If
                End If
            End If
            '
            ' Status
            '
            If TypeOf e.CellElement.RowInfo Is GridViewDataRowInfo Then
                If e.CellElement.ColumnInfo.HeaderText = "Status" Then
                    If e.CellElement.RowInfo.Cells("Batch_Status").Value IsNot Nothing Then
                        Dim strBatchStatus As String = e.CellElement.RowInfo.Cells("Batch_Status").Value.ToString()
                        Dim currentStatusID As Object = e.CellElement.RowInfo.Cells("AccPac_Batch_Status_ID").Value.ToString()
                        If strBatchStatus = "Open" And Not mblnReadOnly Then
                            Dim gvDropDownList As RadDropDownListEditorElement = Nothing
                            If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement) Then
                                e.CellElement.Children.Clear()
                                gvDropDownList = New RadDropDownListEditorElement()
                                For intI = 0 To mstrBatchStatusList.Count - 1
                                    gvDropDownList.Items.Add(New RadListDataItem(mstrBatchStatusList.Item(intI).MyString, mstrBatchStatusList.Item(intI).Id))
                                Next
                                e.CellElement.Children.Add(gvDropDownList)
                                '
                                ' Preset the Drop down
                                '
                                If gvDropDownList IsNot Nothing Then
                                    If currentStatusID IsNot Nothing Then
                                        gvDropDownList.SelectedValue = Convert.ToInt32(currentStatusID)
                                    Else
                                        gvDropDownList.SelectedIndex = -1
                                    End If
                                End If
                            Else
                                gvDropDownList = DirectCast(e.CellElement.Children(0), RadDropDownListEditorElement)
                            End If
                        ElseIf e.CellElement.Children.Count > 0 AndAlso TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement Then
                            '
                            ' Make sure any cells in the status column that are not "Open" status do not get changed to a combobox
                            '
                            e.CellElement.Children.Clear()
                        End If
                    End If
                ElseIf e.CellElement.Children.Count > 0 AndAlso TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement Then
                    '
                    ' Make sure any other columns do not get changed to a combobox
                    '
                    e.CellElement.Children.Clear()
                End If
            End If
        End Sub

    The above code works perfectly to setting the cell to a combobox when the Batch Status is "Open" and the drop down contains the correct values that are in my list array of (mstrBatchStatusList.Item).

    Here are my questions:
    1.  When I want to change my combobox value to a different item in the combobox, first I need to scroll to the right since my column you can't see.  I then change my value and scroll back to the left.  I then scroll back to the right and my combobox becomes reset to the original value.  It looks like upon scrolling right it calls the CellFormating function and when it gets to this line of the code (If e.CellElement.Children.Count = 0 OrElse Not (TypeOf e.CellElement.Children(0) Is RadDropDownListEditorElement) Then) it doesn't recognize that it's currently a combobox so it resets the combo box back to the original status id.

    Why does this happen and how do I get this to not happen?

    2.  If there an event that can be used that triggers a combo box change event so I can check the value it's changing to and set a flag that a value changed in the grid?

    Thanks
  26. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 13 Mar 2014 Link to this post

    Hi,

    As far as I can see your goal is to have a DropDownEditor on the place of the TextEditor. If that is the case, then you need to use a custom editor, instead of adding and removing children in the CellFormating event. We have an article which can of help for this purpose - Using custom editors.

    Also the article Events can be found useful.

    I hope this will be useful.

    Regards,
    Dimitar
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
  27. Erin Arroyo
    Erin Arroyo avatar
    4 posts
    Member since:
    Feb 2010

    Posted 13 Mar 2014 in reply to Dimitar Link to this post

    Hi,
    Thanks for your response and the custom editor worked much better than using the Cell Formatting but in the end I decided to go a different route.
    I created the column from the beginning a GridViewComboBoxColumns.  Then at runtime I dynamically set the data source, value member, display member, and field name.  I then do a check and see if my status is "Open" I leave the combobox enabled else I disable the combo box so it only displays the text of my status.
    Thanks again
  28. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 17 Mar 2014 Link to this post

    Hi Erin,

    Thank you for writing back.

    I am glad that you have found a solution for your case. Do not hesitate to contact us if you have other questions.
     
    Regards,
    Dimitar
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
  29. Rawad
    Rawad avatar
    23 posts
    Member since:
    Jan 2012

    Posted 27 Jun 2014 in reply to Dimitar Link to this post

    Dear

    I'm trying to add a Combobox inside the DataGrid but for certain  rows and columns.
    I'm using the event  CellFormatting, but how can I bind the "RadDropDownListElement".
    I need to Fill it with data


    Here is my code:


        Private Sub RadButton1_Click(sender As System.Object, e As System.EventArgs) Handles RadButton1.Click    
               Dim rowindex = RadGridView1.Rows.AddNew()
                rowindex.Cells(0).Value = 12
                rowindex.Cells(1).Value = "Name"
                rowindex.Cells(2).Value = TODAY

                rowindex = RadGridView1.Rows.AddNew()
                rowindex.Cells(0).Value = 999
                rowindex.Cells(1).Value = "Name"
                rowindex.Cells(2).Value = TODAY
        End Sub


        Private Sub RadGridView1_CellFormatting(sender As System.Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
            If e.CellElement.RowIndex <> -1 Then
                If e.CellElement.ColumnIndex = 0 Then
                    If RadGridView1.Rows(e.CellElement.RowIndex).Cells(e.CellElement.ColumnIndex).Value = "999" Then
                        e.CellElement.Children.Add(New RadDropDownListElement())
                    End If
                End If
            End If
        End Sub







  30. Dimitar
    Admin
    Dimitar avatar
    1404 posts

    Posted 01 Jul 2014 Link to this post

    Hi Ravad,

    Thank you for writing.

    The CellFormatting event is not suitable for such operations since it is fired very often. This event should be used for styling the existing grid elements not for adding new ones.

    To achieve the desired functionality you can use the editors' events. For example you can change the editor depending on the cell value in the EditorRequired event, then you can add a data source to this editor in the CellEditorInitialized event:
    Private Sub radGridView1_CellEditorInitialized(sender As Object, e As GridViewCellEventArgs)
        If TypeOf e.ActiveEditor Is RadDropDownListEditor Then
            Dim editor As RadDropDownListEditor = TryCast(e.ActiveEditor, RadDropDownListEditor)
            Dim element As RadDropDownListEditorElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
            element.DataSource = GetTable1()
     
            element.DisplayMember = "Name"
            element.ValueMember = "ID"
        End If
    End Sub
     
    Private Sub radGridView1_EditorRequired(sender As Object, e As EditorRequiredEventArgs)
        Dim i As Integer = 0
        If Integer.TryParse(radGridView1.CurrentCell.Value.ToString(), i) AndAlso i = 999 Then
            e.EditorType = GetType(RadDropDownListEditor)
            Return
        End If
    End Sub
     

    Please let me know if there is something else I can help you with. 
     
    Regards,
    Dimitar
    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.
     
  31. Denius
    Denius avatar
    11 posts
    Member since:
    Oct 2015

    Posted 09 Nov Link to this post

    Hello, 

    i need help ...

    try to show data from List<> but shows on that column this information System.Collections.Generic.List ' 1[ ]

     

    how to resolve ?

     

Back to Top
UI for WinForms is Visual Studio 2017 Ready