0

I am a beginner programmer developing a C# WinForms / SQL solution in VS 2015 Professional.

Can anybody please help me with the code below? I am trying to implement cascading comboBoxes inside a DataGridView to do the following:

ComboBox1 lists courses the school has; ComboBox 2 lists the modules for the selected course; ComboBox 3 lists the classes for the selected module.

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. Here's the code I'm trying:

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();
            }
        }
    }
  • A DataGridView is a grid. So cascading comboboxes are not really a natural thing here, since they stand for a hierarchical structure. You need a convincing example before you can expect anyone to understand you.. – TaW Oct 10 '16 at 18:08
  • Sorry, I thought I was clear enough... But here's what I'm trying to accomplish: ComboBox 1 is a list of courses - ComboBox 2 is a list of modules for the selected course - ComboBox 3 is a list of classes for the selected module. Is this possible to do? –  Oct 10 '16 at 18:20
  • Asking for a tutorial is off-topic here. If you would like help with unsuccessful code, then change the question to ask for that. – Hank Oct 10 '16 at 18:45
  • _Is this possible to do?_ Yes. But using a DataGridview for this is a weird idea. Using a set of normal Comboboxes makes more sense and is easier to code. – TaW Oct 10 '16 at 19:18
  • [How can you bind a combo box to be dependent on a preceding combobox in a datagridview in Winforms?](http://stackoverflow.com/q/39438171/3110834) – Reza Aghaei Oct 10 '16 at 20:34

0 Answers0