在 datagridview 中显示来自多个表的数据 [英] displaying data from multiple tables in datagridview

查看:25
本文介绍了在 datagridview 中显示来自多个表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一列中的表格列表 n 另一列中的复选框.我想通过单击复选框查看我选择的表的数据

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

我的代码是

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

但是每次只显示1张表的数据.我应该改变什么以及在哪里...?

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

推荐答案

在回答这个问题之前,请您确认一下1) 如果 dataGridView2.Rows[i].Cells[1] 是复选框列2) dataGridView1 是另一个网格,您要在其中显示一个在另一个下方的表格3) 构成dataGridView1 数据源的表是否具有相同顺序的相同列?由于提供的信息不够充分.

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.

当且仅当select *"以相同的顺序给出相同的列,您可以添加多个表.在 for 循环之前创建一个空的数据表(我们称之为 master).在每次迭代中,将新数据表 dbdataset 合并到主数据表.您可以在此处查看语法:链接.这提供了有关如何处理列和主键的详细信息.数据表中的自定义修改也可以在构建后完成.语法在此处 link.希望这会有所帮助!

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

这篇关于在 datagridview 中显示来自多个表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆