第三级联组合框在显示记录时出现问题

时间:2016-10-11 07:56:23

标签: c# combobox

我是一名初学程序员,在VS 2015中开发C#WinForms解决方案。

我成功地级联了三个ComboBox中的两个。问题出在第三个ComboBox上,它没有显示正确的过滤值。它始终显示相同的值。

你能看一下我的代码并告诉我有什么问题吗?我非常感谢您的时间和帮助。谢谢!

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;
using System.Data.SqlClient;

namespace Bremington
{
    public partial class Form1 : Form
    {

        SqlConnection con = new SqlConnection(@;"Data Source=LAPTOP-C30IHOU2;Initial Catalog=BremingtonBackEnd;Integrated Security=True");
        DataRow dr;

        public Form1()
        {
            InitializeComponent();
            refreshCurso();
        }

        public void refreshCurso()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from cursos", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox1.ValueMember = "cod_curso";
            comboBox1.DisplayMember = "curso";
            comboBox1.DataSource = dt;
        }

        public void refreshModulo(int cod_curso)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from modulos where cod_curso= @cod_curso", con);
            cmd.Parameters.AddWithValue("cod_curso", cod_curso);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox2.ValueMember = "cod_modulo";
            comboBox2.DisplayMember = "modulo";
            comboBox2.DataSource = dt;
        }

        public void refreshTurma(int cod_modulo)
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from turmas where cod_modulo= @cod_modulo", con);
            cmd.Parameters.AddWithValue("cod_modulo", cod_modulo);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            con.Close();
            dr = dt.NewRow();
            dt.Rows.InsertAt(dr, 0);
            comboBox3.ValueMember = "cod_turma";
            comboBox3.DisplayMember = "turma";
            comboBox3.DataSource = dt;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex.ToString() != null)
            {
                int cod_curso = Convert.ToInt32(comboBox1.SelectedIndex.ToString());
                refreshModulo(cod_curso);
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex.ToString() != null)
            {
                int cod_modulo = Convert.ToInt32(comboBox2.SelectedIndex.ToString());
                refreshTurma(cod_modulo);
            }
        }

    }
}

0 个答案:

没有答案