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

Changing cell type

3 Answers 126 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 15 Sep 2011, 02:37 PM
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

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 16 Sep 2011, 07:34 AM
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
0
Jason
Top achievements
Rank 1
answered on 16 Sep 2011, 07:15 PM
it works, but after I change one row, I lose my datasource. 
0
Alexander
Telerik team
answered on 20 Sep 2011, 07:15 PM
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 >>

Tags
GridView
Asked by
Jason
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or