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

Switching a row's DataSource on a GridViewComboBoxColumn

3 Answers 77 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Karl B
Top achievements
Rank 1
Karl B asked on 10 Sep 2014, 07:31 PM
I'm trying to swap out the datasource on a ComboBoxColumn depending on other data bound to the row. Below is a simple example to reproduce the problem. (In my actual project we are not using simple strings for the options)

In this example the second row's combo box should have a 3rd option not shown by the first row.
If I let the grid automatically generate columns instead of adding it manually it seems to work correctly. What am I doing wrong?

public class SimpleObject
    {
    public string Option { get; set; }
    public bool ShowOtherOptions { get; set; }
    public SimpleObject (string option)
        {
        Option = option;
        }
    }

Relevant code behind for the form:
private List<String> options = new List<String>(){"First", "Second"};
private List<String> specialoptions = new List<String>(){ "First", "Second", "Third"};
 
public SimpleSample ()
    {
    List<SimpleObject> myList = new List<SimpleObject> ();
    myList.Add (new SimpleObject ("First"));
    myList.Add (new SimpleObject ("Second") {ShowOtherOptions = true});
 
    InitializeComponent ();
 
    this.radGridView1.AutoGenerateColumns = false;
    AddComboColoumn ();
    radGridView1.DataSource = myList;
    radGridView1.EditorRequired += radGridView1_EditorRequired;
    }
 
private void AddComboColoumn()
{
 
    GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("ComboBox column");
    comboColumn.Width = 150;
    comboColumn.FieldName = "Option";
 
    comboColumn.DataSource = options;
    radGridView1.Columns.Add(comboColumn);
}
 
private void radGridView1_EditorRequired (object sender, Telerik.WinControls.UI.EditorRequiredEventArgs e)
    {
        if (radGridView1.CurrentColumn.Name == "Option")
        {
        var data =
            (((Telerik.WinControls.UI.GridViewEditManager) (sender)).GridViewElement.CurrentRow).DataBoundItem
                as SimpleObject;
        RadDropDownListEditor dropdownEditor = new RadDropDownListEditor ();
        if (data.ShowOtherOptions)
            {
            ((RadDropDownListEditorElement) (dropdownEditor.EditorElement)).DataSource = specialoptions;
            }
        else
            {
            ((RadDropDownListEditorElement) (dropdownEditor.EditorElement)).DataSource = options;
            }
 
        dropdownEditor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
        e.Editor = dropdownEditor;
 
        }
    }


3 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 15 Sep 2014, 07:54 AM
Hello Karl,

Thank you for contacting us.

Your approach is correct, but in this case you should use the CellEditorInitialized event to set the data source:
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (radGridView1.CurrentColumn.Name == "Option")
    {
        var data =
            (((Telerik.WinControls.UI.GridViewEditManager)(sender)).GridViewElement.CurrentRow).DataBoundItem
            as SimpleObject;
        RadDropDownListEditor dropdownEditor = e.ActiveEditor as RadDropDownListEditor;
        if (data.ShowOtherOptions)
        {
            ((RadDropDownListEditorElement)(dropdownEditor.EditorElement)).DataSource = specialoptions;
        }
        else
        {
            ((RadDropDownListEditorElement)(dropdownEditor.EditorElement)).DataSource = options;
        }
         
        dropdownEditor.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
    }
}

I hope this helps. Should you have any other questions do not hesitate to ask.
 
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.
 
0
Karl B
Top achievements
Rank 1
answered on 15 Sep 2014, 04:46 PM
Dimitar,
That solves the problem of displaying the correct dropdown options, thank you. Now I have come across another problem. The first time I click on the second row, the drop down editor does not display the correct selected item (it defaults to the first). After selecting a new item in the drop down, the editor syncs correctly.  I suspect this has to do with the fact the column's DataSource does not match the editor. Is there a way to initialize the editor's selected item to match the cell value?
0
Accepted
Dimitar
Telerik team
answered on 18 Sep 2014, 09:44 AM
Hi Karl,

Thank you for writing back.

In this case you can set the selected value after you have set the datasource in the CellEditorInitialized event (this is necessary since resetting the data source is resenting the selected item as well) :
void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    if (radGridView1.CurrentColumn.Name == "Option")
    {
       //other implementation omitted
        ((RadDropDownListEditorElement)(dropdownEditor.EditorElement)).SelectedValue = radGridView1.CurrentCell.Value;
    }
}

Do not hesitate to contact us if you have other questions.
 
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.
 
Tags
GridView
Asked by
Karl B
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Karl B
Top achievements
Rank 1
Share this question
or