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

Help Me..RadGridComboBox Problem

3 Answers 191 Views
GridView
This is a migrated thread and some comments may be shown as answers.
mampus
Top achievements
Rank 1
mampus asked on 01 Nov 2010, 06:01 AM
Hi there..
I have 2 radgridcombobox, the first one calls "faculty" and the seconds calls "department"...
How i link those combo box?? so, When I choose the index 0 of faculty, then the "department" load stuff like "Industry, physics..etc.."...??

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 01 Nov 2010, 10:20 PM
Hello mampus,

If you mean you want cascading dropdowns, please take a look at this thread under Nikolay's reply there is a project attached that does just this.

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

Best Regards,
Emanuel Varga
0
mampus
Top achievements
Rank 1
answered on 08 Nov 2010, 04:26 AM
hi there...
umm...i still don't understand how to do it...???

I will clear the question...i have an table looks like this:

id Name Faculty(gridcombo box1)  Department(gridCombo box2)
1 steve Engineering (if gridcombobox1 = engineering then show ("Civil, Industry, Physics..."))
2 mary Design ("Interior, Architech, etc....")
how i can do that??
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 08 Nov 2010, 10:47 AM
Hello Mampus,

Please try the following example:
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    private RadGridView radGridView1;
    private List<Teacher> teachers;
    private List<Faculty> faculties = new List<Faculty>
    {
        new Faculty { Id = 1, Name = "Engineering"},
        new Faculty { Id = 2, Name = "Design"}
};
 
    private List<Department> departments = new List<Department>
    {
        new Department { Id = 1, FacultyId = 1, Name = "Civil"},
        new Department { Id = 2, FacultyId = 1, Name = "Industry"},
        new Department { Id = 3, FacultyId = 1, Name = "Physics"},
        new Department { Id = 4, FacultyId = 2, Name = "Interior"},
        new Department { Id = 5, FacultyId = 2, Name = "Architecht"},
};
 
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(radGridView1 = new RadGridView());
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        radGridView1.Dock = DockStyle.Fill;
 
        teachers = new List<Teacher>() {
            new Teacher { Id = 1, Name = "john", DepartmentId = 4, FacultyId = 2 },
            new Teacher { Id = 2, Name = "mary", DepartmentId = 2, FacultyId = 1 }
        };
 
        radGridView1.DataBindingComplete += new GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
        radGridView1.CellValueChanged += new GridViewCellEventHandler(radGridView1_CellValueChanged);
        radGridView1.CellEditorInitialized += new GridViewCellEventHandler(radGridView1_CellEditorInitialized);
    }
 
    void radGridView1_CellValueChanged(object sender, GridViewCellEventArgs e)
    {
        if (e.Column.Name == "Faculty")
        {
            var selectedDepartmentId = e.Row.Cells["Department"].Value;
            var departmentsForFaculty = departments.Where(dep => dep.FacultyId.Equals(e.Value)).ToArray();
            if (!departmentsForFaculty.Any(dep => dep.Id.Equals(selectedDepartmentId)))
            {
                e.Row.Cells["DepartmentId"].Value = departmentsForFaculty[0].Id;
            }
        }
    }
 
    void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
    {
        if (e.Column.Name == "Department")
        {
            var dropDownEditor = radGridView1.ActiveEditor as RadDropDownListEditor;
            var element = dropDownEditor.EditorElement as RadDropDownListEditorElement;
            element.DataSource = departments.Where(dep => dep.FacultyId.Equals(e.Row.Cells["Faculty"].Value));
            element.ShowPopup();
        }
    }
 
    protected override void OnLoad(System.EventArgs e)
    {
        base.OnLoad(e);
        radGridView1.DataSource = teachers;
    }
 
    void radGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
    {
        var grid = sender as RadGridView;
        if (grid == null)
        {
            return;
        }
 
        var comboBoxColumnFaculty = new GridViewComboBoxColumn("Faculty", "FacultyId");
        comboBoxColumnFaculty.DisplayMember = "Name";
        comboBoxColumnFaculty.ValueMember = "Id";
 
        // set the data source for the faculties here
        comboBoxColumnFaculty.DataSource = faculties;
        grid.Columns.Add(comboBoxColumnFaculty);
        grid.Columns["FacultyId"].IsVisible = false;
 
        var comboBoxColumnDepartment = new GridViewComboBoxColumn("Department", "DepartmentId");
        comboBoxColumnDepartment.DisplayMember = "Name";
        comboBoxColumnDepartment.ValueMember = "Id";
        comboBoxColumnDepartment.DataSource = departments;
        grid.Columns.Add(comboBoxColumnDepartment);
        grid.Columns["DepartmentId"].IsVisible = false;
    }
 
    public class Teacher
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
 
        public int FacultyId
        {
            get;
            set;
        }
 
        public int DepartmentId
        {
            get;
            set;
        }
    }
 
    public class Faculty
    {
        public int Id
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
    }
 
    public class Department
    {
        public int Id
        {
            get;
            set;
        }
 
        public int FacultyId
        {
            get;
            set;
        }
 
        public string Name
        {
            get;
            set;
        }
    }
}

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

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
mampus
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
mampus
Top achievements
Rank 1
Share this question
or