displaying data from multiple tables in datagridview

六月ゝ 毕业季﹏ 提交于 2020-01-03 05:38:05

问题


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..?


回答1:


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


来源:https://stackoverflow.com/questions/24743963/displaying-data-from-multiple-tables-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!