【发布时间】:2018-07-09 16:54:52
【问题描述】:
我正在制作 Windows 应用程序并且卡在一个地方。
我的问题是我想通过选择ComboBox 项目在DataGridView 中显示记录,但我不明白正确的方法。请帮我解决这个问题。
private void grid_Load(object sender, EventArgs e)
{
con = new SqlConnection(constr);
try
{
con.Open();
//this.studTableAdapter.Fill(this.pRJTestDBDataSet.stud);
//above line show error for connection to database
da = new SqlDataAdapter("SELECT stud_no FROM stud", con);
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "stud_no";
comboBox1.ValueMember = "stud_no";
comboBox1.DataSource = dt;
comboBox1.SelectedIndex = -1;
comboBox1_SelectedIndexChanged(sender, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{ con.Close(); }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.studTableAdapter.Fill(pRJTestDBDataSet.stud);
//above line show error for connection to database
}
我已经尝试了上面的代码,但是它在那里不起作用,比如登录失败用户
【问题讨论】:
-
你应该分享一些你已经尝试过的代码。
标签: c# datagridview combobox