Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 第三个级联组合框显示记录时出现问题_C#_Combobox - Fatal编程技术网

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

C# 第三个级联组合框显示记录时出现问题,c#,combobox,C#,Combobox,我是一名在VS2015中开发C#WinForms解决方案的初学者程序员 我成功地级联了三个组合框中的两个。问题在于第三个组合框没有显示正确的过滤值。它始终显示相同的值 你能看一下我的密码,告诉我怎么了吗?我真的很感谢你的时间和帮助。谢谢大家! using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst

我是一名在VS2015中开发C#WinForms解决方案的初学者程序员

我成功地级联了三个组合框中的两个。问题在于第三个组合框没有显示正确的过滤值。它始终显示相同的值

你能看一下我的密码,告诉我怎么了吗?我真的很感谢你的时间和帮助。谢谢大家!

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);
            }
        }

    }
}

第三个
comboBox3\u SelectedIndexChanged
事件在哪里?在一个不相关的注释中,
Convert.ToInt32(comboBox2.SelectedIndex.ToString())
Combox2.SelectedIndex)相同吗?
?相同的问题。第三个组合框在哪里?因为您是初学者,我将给您最好的建议:学会调试。在代码执行之后放置断点,这样您就可以看到发生了什么。在你的例子中,我会在
Refreshterma
方法中放置一个断点,看看它是否到达那里,以及你得到了什么
cod\u模
comboBox3是循环中的最后一个Combox3,所以我认为它不需要SelectedIndexChanged事件…你的第三个
Combox3\u SelectedIndexChanged
事件在哪里,
Convert.ToInt32(comboBox2.SelectedIndex.ToString())
是否与
comboBox2.SelectedIndex)相同。第三个组合框在哪里?因为您是初学者,我将给您最好的建议:学会调试。在代码执行之后放置断点,这样您就可以看到发生了什么。在您的情况下,我会在
refreshterma
方法中放置一个断点,看看它是否到达那里,以及您得到了什么
cod\u模
Combobox3是循环中的最后一个组合框,所以我认为它不需要SelectedIndexChanged事件。。。