简体   繁体   English

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

[英]How to populate a DataGridView by selecting any table from combo box

I have used the following code for displaying names of my SQL db tables in a combo box. 我使用以下代码在组合框中显示我的SQL数据库表的名称。

Now I want that when I click on any of these table names from the combo box, my DGV populates with that table's contents. 现在我希望当我从组合框中点击这些表名中的任何一个时,我的DGV会填充该表的内容。

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

Then I used the following code for populating my DGV but it's not working; 然后我使用以下代码填充我的DGV,但它不起作用; please help. 请帮忙。

private void PopulateGridView()
{
    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 " + comboBox1.SelectedText;

        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);
        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.DataSource = dtRecord;
        dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
        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());
    }
}

Try this: 尝试这个:

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBox1.SelectedValue != null)
        {
           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 " + comboBox1.SelectedValue;
                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = dtRecord;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

just try this: update your code in the Form Load with below: 试试这个:在下面的表单加载中更新你的代码:

 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";
        comboBox1.ValueMember = "table_name";
        con.Close();
   }
   catch (Exception ex)
   {
        MessageBox.Show(ex.Message);
   }
}

so basically you have added one extra line comboBox1.ValueMember = "table_name"; 所以基本上你添加了一个额外的行comboBox1.ValueMember = "table_name";

and make your PopulateGridView method like this: 并使你的PopulateGridView方法像这样:

   private void PopulateGridView(string tblName)
        {
            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 " + tblName;

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = dtRecord;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

it has to work. 它必须工作。 Also, I see, you are creating SqlConnection object everywhere, including SqlCommand and SqlDataAdapter . 另外,我知道,你到处都在创建SqlConnection对象,包括SqlCommandSqlDataAdapter

try to wrap them up in static methods ie 尝试用静态方法包装它们,即

 - public static SqlConnection OpenConnection()
 - public static DataTable ExecuteSelectQuery(string Query)
 - public static bool ExecuteModifyQuery(string Query)

try to write as less amount of code as you can. 尝试尽可能少地编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过从组合框中选择任何表来填充datagridview - Populate datagridview by selecting any table from combo box 如何从另一个组合框填充相同形式的组合框? - How to populate a combo box from another combo box in the same form? 如何根据datagridview中另一个组合框中的选定值填充datagridview中的组合框? - How to populate a combo box in a datagridview as per the selected values in another combobox in datagridview? 如何将大容量数据从数据库表填充到组合框? -C#.NET - How Can i Populate Bulk of Data from a DB Table into a Combo Box? - C#.NET 如何将组合框放在从Sql查询填充的DataGridView中? - How to put a combo box inside the DataGridView that is populated from a Sql query? 从组合框中选择一个项目后,在datagridview中显示对象数据的单行 - Display single row of object data in a datagridview after selecting an item from combo box 如何根据从另一个组合框所做的选择填充组合框 - How to populate a combo box based on the selection made from another combo box 组合框填充了valueMember而不是DataGridView中的DisplayMember吗? - Combo box is filling with valueMember not with the DisplayMember from DataGridView? C#从方法填充组合框 - C# populate a combo box from a method 使用组合框从类中选择和检索数据 - Selecting and retrieving data from a class with a combo box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM