【发布时间】:2014-04-01 01:45:57
【问题描述】:
我正在开发一个应用程序,其中我有四个组合框(即组合框 1 用于类,组合框 2 用于组,组合框 3 用于部分,组合框 4 用于月份)和一个列表框。我已经从数据库中获取数据到“OnLoad 函数”中的所有这些组合框。现在我想根据组合框在列表框中显示学生姓名。即当我从类(comboBox1)中选择“第 7 类”时。它应该显示那些在选定班级(第 7 班)、选定组(在组合框 2 中选定)和选定部分(在组合框 3 中选定)的学生。这是我的代码,但它给了我 SQL 语法异常和 indexOutOfRange 异常。附加的数据库在mysql中。 “dbOperation”是“myDatabse”类的对象,我在其中连接了数据库并实现了所有查询。
public partial class FeeVoucherPrint : Form
{
myDatabase dbOperation = new myDatabase();
DataTable table = new DataTable();
public FeeVoucherPrint()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
}
private void FeeVoucherPrint_Load(object sender, EventArgs e)
{
table = dbOperation.select("* from class");
comboBox1.DataSource = table;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "id";
table = dbOperation.select("* from `group`");
comboBox2.DataSource = table;
comboBox2.DisplayMember = "name";
comboBox2.ValueMember = "id";
table = dbOperation.select("* from section");
comboBox3.DataSource = table;
comboBox3.DisplayMember = "name";
comboBox3.ValueMember = "id";
table = dbOperation.select("* from months");
comboBox4.DataSource = table;
comboBox4.DisplayMember = "month";
comboBox4.ValueMember = "id";
}
private void fill()
{
try
{
table = dbOperation.select("studentid from studentinclass where classid = " + comboBox1.SelectedValue + " and groupid = " + comboBox2.SelectedValue);
table = dbOperation.select("* from student where studid = " + table.Rows[0]["studentid"]);
listBox1.DataSource = table;
listBox1.DisplayMember = "name";
listBox1.ValueMember = "studid";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
fill();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
fill()
}
}
【问题讨论】:
-
假设 dbOperation 返回一个数据表或一个 ienumerable ,你应该将它直接分配给数据源。
标签: c# mysql winforms combobox