C# 第三个级联组合框显示记录时出现问题
我是一名在VS2015中开发C#WinForms解决方案的初学者程序员 我成功地级联了三个组合框中的两个。问题在于第三个组合框没有显示正确的过滤值。它始终显示相同的值 你能看一下我的密码,告诉我怎么了吗?我真的很感谢你的时间和帮助。谢谢大家!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
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事件。。。