通过从组合框中选择任何表来填充datagridview

时间:2013-03-11 10:21:28

标签: c# sql-server

我使用以下代码在combobox中显示我的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

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

你将在combobox SelectedIndexChanged上有一个事件,并将表的名称传递给你的函数

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);
            }
}