Home / Community & Support / Knowledge Base / RadControls for WinForms / GridView / RadComboBoxEditor in GridViewComboBoxColumn dependant on the value of another cell

RadComboBoxEditor in GridViewComboBoxColumn dependant on the value of another cell

Article Info

Rating: Not rated


Article information

Article relates to

 Q3 2010 SP 1

Created by

 Stefan Stefanov

Last modified

 January 3, 2011

Last modified by

 


HOW-TO
This article demonstrates how you can make a RadComboBoxEditor in GridViewComboBoxColumn dependant on the value of another cell. 

SOLUTION
1. Lets created two classes that are going to be used in this example. The first is called FoodType and the other one is Food. As the classes names speak we will have two combo boxes - the first will allow choosing the type of the food (i.e. Fruits and Vegetables) and the second the food itself (i.e Cucumbers, Bananas etc):
public class FoodType
{
    private int foodTypeID;
    private string foodType;
 
    public FoodType(int foodTypeID, string foodType)
    {
        this.foodTypeID = foodTypeID;
        this.foodType = foodType;
    }
 
    public int FoodTypeID
    {
        get { return foodTypeID; }
        set { foodTypeID = value; }
    }
 
    public string FoodTypeName
    {
        get { return foodType; }
        set { foodType = value; }
    }
}
 
public class Food
{
    private int foodID;
    private string foodName;
 
    public Food(int foodID, string foodName)
    {
        this.foodID = foodID;
        this.foodName = foodName;
    }
 
    public int FoodID
    {
        get { return foodID; }
        set { foodID = value; }
    }
 
    public string FoodName
    {
        get { return foodName; }
        set { foodName = value; }
    }
}

2. Create three BindingLists (as fields) that will hold the necessary data and populate them
BindingList<Food> fullList;
BindingList<Food> fruitsList;
BindingList<Food> vegetablesList;
 
public Form1()
{
    InitializeComponent();
 
    fullList = new BindingList<Food>();
    fullList.Add(new Food(0, "Onion"));
    fullList.Add(new Food(1, "Cucumber"));
    fullList.Add(new Food(2, "Tomato"));
    fullList.Add(new Food(3, "Peach"));
    fullList.Add(new Food(4, "Banana"));
    fullList.Add(new Food(5, "Grape"));
 
    fruitsList = new BindingList<Food>();
    fruitsList.Add(fullList[3]);
    fruitsList.Add(fullList[4]);
    fruitsList.Add(fullList[5]);
 
    vegetablesList = new BindingList<Food>();
    vegetablesList.Add(fullList[0]);
    vegetablesList.Add(fullList[1]);
    vegetablesList.Add(fullList[2]);
 
    BindingList<FoodType> typesList = new BindingList<FoodType>();
    typesList.Add(new FoodType(0, "Vegetables"));
    typesList.Add(new FoodType(1, "Fruits"));
 
    GridViewComboBoxColumn foodType = new GridViewComboBoxColumn();
    foodType.FieldName = "FoodType";
    this.radGridView1.Columns.Add(foodType);
    foodType.DataSource = typesList;
    foodType.Width = 100;
    foodType.DisplayMember = "FoodTypeName";
    foodType.ValueMember = "FoodTypeID";
 
    GridViewComboBoxColumn food = new GridViewComboBoxColumn();
    food.FieldName = "Food";
    this.radGridView1.Columns.Add(food);
    food.DataSource = fullList;
    food.Width = 100;
    food.DisplayMember = "FoodName";
    food.ValueMember = "FoodID";
}

3. Create the GridViewComboBoxColumn columns and make their settings. The first column is responsible for the food types, while the second for the food according to the food type:
GridViewComboBoxColumn foodType = new GridViewComboBoxColumn();
foodType.FieldName = "FoodType";
this.radGridView1.Columns.Add(foodType);
foodType.DataSource = typesList;
foodType.Width = 100;
foodType.DisplayMember = "FoodTypeName";
foodType.ValueMember = "FoodTypeID";
 
GridViewComboBoxColumn food = new GridViewComboBoxColumn();
food.FieldName = "Food";
this.radGridView1.Columns.Add(food);
food.DataSource = fullList;
food.Width = 100;
food.DisplayMember = "FoodName";
food.ValueMember = "FoodID";

4. Last subscribe for the CellEditorInitialized event of RadGridView, where modifications for the dynamic editor will be placed:
void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    if (e.Column.HeaderText == "Food")
    {
        if (this.radGridView1.CurrentRow.Cells["FoodType"].Value != DBNull.Value
            && this.radGridView1.CurrentRow.Cells["FoodType"].Value != null)
        {
            RadDropDownListEditor editor = (RadDropDownListEditor)this.radGridView1.ActiveEditor;
            RadDropDownListEditorElement editorElement = (RadDropDownListEditorElement)editor.EditorElement;
            if (int.Parse(this.radGridView1.CurrentRow.Cells["FoodType"].Value.ToString()) == 0)
            {
                editorElement.DataSource = vegetablesList;
            }
            else
            {
                editorElement.DataSource = fruitsList;
            }
            editorElement.SelectedValue = null;
            editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;
        }
    }
}

The result is:
                                    

Comments

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.