0

我使用以下代码在组合框中显示我的 SQL 数据库表的名称。现在,当我从组合框中单击任何这些表名时,我想要我的 DataGridView 填充该表的内容。

我的数据库中有三个表。

private void Form1_Load(object sender, EventArgs e)
    {
        String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

        SqlConnection con = new SqlConnection(strConnection);
        try
        {

            con.Open();

            SqlCommand sqlCmd = new SqlCommand();

            sqlCmd.Connection = con;
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "Select table_name from information_schema.tables";

            SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

            DataTable dtRecord = new DataTable();
            sqlDataAdap.Fill(dtRecord);
            comboBox1.DataSource = dtRecord;
            comboBox1.DisplayMember = "TABLE_NAME";
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void PopulateGridView(string tablename)
    {
        String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

        SqlConnection con = new SqlConnection(strConnection);
        try
        {

            con.Open();

            SqlCommand sqlCmd = new SqlCommand();

            sqlCmd.Connection = con;
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "Select * from " + tablename;

            SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

            DataTable dtRecord = new DataTable();
            sqlDataAdap.Fill(dtRecord);
            dataGridView1.DataSource = dtRecord;

            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedValue != null)
        {
            PopulateGridView(comboBox1.SelectedValue.ToString());
        }

    }
}

}

这是我的表 (datejoin)

rahul   sharma  12-1-2011
suresh  chand   23-4-2012
prachi  shukla  13-2-2011
siddharth   malhotra    25-9-2012
saina   chopra  12-8-2011
amit    mehta   20-3-2012
4

2 回答 2

1

订阅组合框的SelectedIndexChanged事件并从组合框中检索选定的表名。从那里,运行查询以从表中检索记录并将结果绑定到 datagridview。

样本:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

    SqlConnection con = new SqlConnection(strConnection);
    con.Open();

    string tableName = comboBox1.SelectedText;

    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.Connection = con;
    sqlCmd.CommandType = CommandType.Text;
    sqlCmd.CommandText = "Select * from " + tableName;

    SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

    DataTable dtRecord = new DataTable();
    sqlDataAdap.Fill(dtRecord);
    dgv.DataSource = dtRecord;
    con.Close();
}
于 2013-03-11T10:25:48.990 回答
1

您将在组合框 SelectedIndexChange 上有一个事件并将表的名称传递给您的函数

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   RebindGridView(ComboBox1.SelectedValue);
}

然后在你的函数中:

private void RebindGridView(string tablename)
{
  String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

            SqlConnection con = new SqlConnection(strConnection);
            try
            {

                con.Open();

                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "Select * from " + tablename;

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                gridview1.DataSource = dtRecords;
                 gridview1.DataBind();
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}
于 2013-03-11T10:32:06.297 回答