Changing cell type

4 posts, 0 answers
  1. Jason
    Jason avatar
    3 posts
    Member since:
    Aug 2011

    Posted 15 Sep 2011 Link to this post

    Hello,
         I want to dynamically change the type of cell in the current row from TextBox to RadDropDownList.  I need the DropDownList to already be populated so I created one in the designer view and just made it not visible.  How do I populate it though?  I can't seem to access it in code.
    Thanks!
    Jason
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 16 Sep 2011 Link to this post

    Hello Jason,

    You don't need to change the cell type in this scenario, you just need to change the editor assigned to that cell.

    To do this, you have to handle 2 events, the CellEditorRequired event, where you will say that for this column you want an editor of this type, and after that you have to handle the CellEditorInitialized event where again, if it's that column, set the data source for the editor + any other things you need.

    Something like this:
    void radGridView1_EditorRequired(object sender, EditorRequiredEventArgs e)
    {
        if (radGridView1.CurrentColumn.FieldName == "SomeField")
        {
            e.EditorType = typeof (RadDropDownListEditor);
        }
         
    }
     
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (radGridView1.CurrentColumn.FieldName == "SomeField")
        {
            var editor = e.ActiveEditor as RadDropDownListEditor;
            var element = editor.EditorElement as RadDropDownListEditorElement;
            element.DataSource = someDataSource; // set the datasource here
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jason
    Jason avatar
    3 posts
    Member since:
    Aug 2011

    Posted 16 Sep 2011 Link to this post

    it works, but after I change one row, I lose my datasource. 
  5. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 20 Sep 2011 Link to this post

    Hello Jason,

    To keep the items in the RadDropDownListEditor DataSource, when using the Emanuel's solution, you should specify the editor's BindingContext:
    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (this.radGridView1.CurrentColumn.Name == "Column1")
        {
            var editor = e.ActiveEditor as RadDropDownListEditor;
            var element = editor.EditorElement as RadDropDownListEditorElement;
            element.BindingContext = this.BindingContext;
            element.DataSource = this.comboData; // set the datasource here
        }
    }

    I hope it helps.

    Best regards,
    Alexander
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top