简体   繁体   English

在datagridview中显示来自多个表的数据

[英]displaying data from multiple tables in datagridview

I have a list of tables in one column n checkboxes in another column. 我在一个列中有一个表列表,在另一列中有n复选框。 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. 但是每次只显示1个表的数据。 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? 在可以回答这个问题之前,您可以先确认1)dataGridView2.Rows [i] .Cells [1]是否为复选框列2)dataGridView1是另一个网格,您要在其中另一个下面显示表格3)做表格形成dataGridView1的数据源的列以相同的顺序排列? 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. 在for循环之前创建一个空的数据表(称为主表)。 In each iteration, merge the new datatable dbdataset to the master datatable. 在每次迭代中,将新的数据表dbdataset合并到主数据表中。 You can view the syntax here: link . 您可以在此处查看语法: 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 . 语法在这里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();

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

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