Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a beginner programmer developing a C# WinForms / SQL solution in VS 2015 Professional.

Does anybody know of any good tutorials on how to create cascading comboBoxes inside a datagridview? I've googled extensively but nothing I've tried has worked so far... This is so frustrating!

Thank you. I really appreciate your time and help.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bremington
{
    public partial class Alunos : Form
    {
        public Alunos()
        {
            InitializeComponent();
        }

        BindingSource filteredModulosBS = new BindingSource();
        BindingSource filteredTurmasBS = new BindingSource();


        private void Alunos_Load(object sender, EventArgs e)
        {
            this.alunosTableAdapter.Fill(this.bremingtonBackEndDataSet.alunos);
            this.alunos_detTableAdapter.Fill(this.bremingtonBackEndDataSet.alunos_det);
            this.cursosTableAdapter.Fill(this.bremingtonBackEndDataSet.cursos);
            this.modulosTableAdapter.Fill(this.bremingtonBackEndDataSet.modulos);
            this.turmasTableAdapter.Fill(this.bremingtonBackEndDataSet.turmas);

            DataView dvM = new DataView(bremingtonBackEndDataSet.Tables["modulos"]);
            filteredModulosBS.DataSource = dvM;

            DataView dvT = new DataView(bremingtonBackEndDataSet.Tables["turmas"]);
            filteredTurmasBS.DataSource = dvT;
        }

        private void alunos_detDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == ComboBoxColumnModulo.Index)
            {
                DataGridViewComboBoxCell dgcbm = (DataGridViewComboBoxCell)alunos_detDataGridView[e.ColumnIndex, e.RowIndex];
                dgcbm.DataSource = filteredModulosBS;
                this.filteredModulosBS.Filter = "cod_curso='" + this.alunos_detDataGridView[e.ColumnIndex - 1, e.RowIndex].Value.ToString() + "'";
            }

             if (e.ColumnIndex == ComboBoxColumnTurma.Index)
             {
                DataGridViewComboBoxCell dgcbt = (DataGridViewComboBoxCell)alunos_detDataGridView[e.ColumnIndex, e.RowIndex];
                dgcbt.DataSource = filteredTurmasBS;
                this.filteredTurmasBS.Filter = "cod_modulo='" + this.alunos_detDataGridView[e.ColumnIndex - 1, e.RowIndex].Value.ToString() + "'";
            }
        }

        private void alunos_detDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == this.ComboBoxColumnModulo.Index)
            {
                DataGridViewComboBoxCell dgcbm = (DataGridViewComboBoxCell)alunos_detDataGridView[e.ColumnIndex, e.RowIndex];
                dgcbm.DataSource = modulosBindingSource;
                this.filteredModulosBS.RemoveFilter();
            }

            if (e.ColumnIndex == this.ComboBoxColumnTurma.Index)
            {
                DataGridViewComboBoxCell dgcbt = (DataGridViewComboBoxCell)alunos_detDataGridView[e.ColumnIndex, e.RowIndex];
                dgcbt.DataSource = turmasBindingSource;
                this.filteredTurmasBS.RemoveFilter();
            }
        }
    }
}
Posted
Updated 10-Oct-16 8:28am

1 solution

Not sure for what you exactly looking for but a google search served me with so many results to articles/blogs/forum posts relevant to your query. Check following if they help-
c# - How to implement cascading ComboBoxes in DataGridView control? - Stack Overflow[^]
http://demos.devexpress.com/ASPxgridviewDemos/GridEditing/CascadingComboBoxes.aspx[^]
Cascading ComboBox In Windows Application Using C#[^]

If I am looking things in a different direction than you wanted, please educate me.

Thanks :)
 
Share this answer
 
Comments
JC Carmo 10-Oct-16 17:06pm    
Hi, Suvenu. Thanks for your reply. I have also found many results, but as yours, they are not about cascading comboboxes in a DataGridView and C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900