简体   繁体   中英

displaying data from multiple tables in datagridview

I have a list of tables in one column n checkboxes in another column. I want to view data of the tables which I select by clicking on the checkboxes

My code is

        for (int i = 0; i < dataGridView2.Rows.Count; i++ )
        {
            if (dataGridView2.Rows[i].Cells[1].Value != null)
            {
                if ((Boolean)dataGridView2.Rows[i].Cells[1].Value == true)
                {
                    try
                    {
                        string myConnection="datasource=localhost;database=dmrc;port=3306;username=root;password=root";
                        MySqlConnection myConn = new MySqlConnection(myConnection);
                        string query = "select * from dmrc." + dataGridView2.Rows[i].Cells[0].Value.ToString();
                        MySqlCommand cmdDatabas = new MySqlCommand(query, myConn);
                        MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                        myDataAdapter.SelectCommand = cmdDatabas;
                        DataTable dbdataset = new DataTable();
                        myDataAdapter.Fill(dbdataset);
                        BindingSource bSource = new BindingSource();
                        bSource.DataSource = dbdataset;
                        f1.dataGridView1.DataSource = bSource;
                        myDataAdapter.Update(dbdataset);
                        f1.Show();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }

But each time it shows the data of 1 table only. What should I change and where..?

Before this question can be answered can you please confirm 1) if dataGridView2.Rows[i].Cells[1] is the checkbox column 2) dataGridView1 is another grid where you want to display the tables one below the other 3) Do the tables that form the datasource of dataGridView1 have the same columns in the same order? As the information provided isn't sufficient.

If, and only if, the "select *" gives the same columns in the same order, you can add multiple tables. Create an empty datatable (lets call it master) before the for loop. In each iteration, merge the new datatable dbdataset to the master datatable. You can view the syntax here: link . This gives details on how you can handle your columns and primary keys. Custom modifications in datatable can also be done AFTER you have built it. The syntax is here link . Hope this helps!

DataTable masterdbdataset = new DataTable();
//Perform custom operations here if necessary
    BindingSource bSource = new BindingSource();

    for (int i = 0; i < dataGridView2.Rows.Count; i++ )
    {
        if (dataGridView2.Rows[i].Cells[1].Value != null)
        {
            if ((Boolean)dataGridView2.Rows[i].Cells[1].Value == true)
            {
                try
                {
                    string myConnection="datasource=localhost;database=dmrc;port=3306;username=root;password=root";
                    MySqlConnection myConn = new MySqlConnection(myConnection);
                    string query = "select * from dmrc." + dataGridView2.Rows[i].Cells[0].Value.ToString();
                    MySqlCommand cmdDatabas = new MySqlCommand(query, myConn);
                    MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
                    myDataAdapter.SelectCommand = cmdDatabas;
                    DataTable dbdataset = new DataTable();
                    myDataAdapter.Fill(dbdataset);
                    myDataAdapter.Update(dbdataset);
                    masterdbdataset.Merge(dbdataset);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
}
    bSource.DataSource = masterdbdataset;
    f1.dataGridView1.DataSource = bSource;
    f1.Show();

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