通过从组合框中选择任意表来填充数据视图

本文关键字:任意表 填充 数据 视图 选择 组合 | 更新日期: 2023-09-27 18:03:22

我使用以下代码在组合框中显示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

通过从组合框中选择任意表来填充数据视图

订阅组合框的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();
}

您将在combobox 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);
            }
}