1

我正在制作 Windows 应用程序并被困在一个地方。我的问题是我想DataGridView通过选择一个ComboBox项目来显示记录,但我不明白正确的方法。请帮助我克服这个问题。

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
}

我已经尝试了上面的代码,但它在那里不起作用,例如登录失败用户

4

3 回答 3

2
    cmd = new SqlCommand("SELECT stud_no FROM stud", con);
    da = new SqlDataAdapter(cmd);

    da.Fill(dt);
    Combobox1.DataSource = dt;
    Combobox1.DisplayMember = dt.Columns("Stud_no").ToString;
于 2012-09-10T08:30:16.823 回答
0

通过要绑定的数据在 Combo Box 的每个 SelectedItemIndex 更改事件处重新绑定 DataGrid。

于 2012-05-01T03:31:21.617 回答
-1
 private void button2_Click(object sender, EventArgs e)//button 2 is a show data button
    {
        if (combo_floor.Text != "")
        {

            DataSet ds = new DataSet();
            string sql = "select floor_id,floor_no,floor_remark,floor_entrydate from Floorinfo where floor_no='"+combo_floor.Text+"'";
            ds = c.select_query(sql);
            dataGridView1.DataSource = ds.Tables["a"];
            combo_floor.Text = "";
        }

        else
        {
            showdata();
            //showdata()is made for show all data from the given table name
        }


    }

//连接在不同的类所以请不要介意

于 2018-07-09T08:54:52.553 回答