简体   繁体   中英

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.

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.

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; 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";

and make your PopulateGridView method like this:

   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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM