从datagridview的多个表显示数据 [英] displaying data from multiple tables in datagridview

查看:229
本文介绍了从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

我的code是

        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 [Ⅰ] .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.

如果,且仅当选择*给出了相同的顺序相同的列,您可以添加多个表。创建一个空的DataTable(可以称之为大师)前循环。在每次迭代中,合并新的DataTable dbdataset到主数据表。你可以在这里查看语法:<一href=\"http://social.msdn.microsoft.com/Forums/en-US/f122d7f4-3b7d-4d93-bd0f-8bb57cd990a4/how-to-join-two-datatables-using-c-no-database-access?forum=adodotnetdataset\"相对=nofollow>链接。这使你可以如何处理您的列和主键的详细信息。在数据表自定义的修改,也可以完成后,你已经把它。语法是这里链接
希望这有助于!

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天全站免登陆