如何从c#中的一个mysql表中填充两个不同的datagrid

时间:2015-06-15 13:56:48

标签: c# mysql wpf datagrid ado.net

我的申请表有问题。我想从一个mysql表中使用WPF在C#中填充两个不同的DataGrid

我不知道如何正确地做到这一点。下面是我的示例代码,它不起作用。它目前仅填充czesciTables,但不填充czesciTables2

我一直在研究如何做到这一点,但谷歌只向我展示了其他来源的结果。

try
{
    for (int i = 0; i <= 4; i++ )
    {
        connection.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT id_czesci_symbol AS KOD,
                                                    ilosc AS ILOSC
                                             FROM `test`.`zamowienie`
                                             WHERE z_numer_naprawy='" + numberBox.Content.ToString() + "'
                                             ORDER BY ilosc LIMIT 5;", connection);
        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds, "LoadDataBinding");
        czesciTable.DataContext = ds;
    }

    for (int i = 5; i <= 9; i++)
    {
        connection.Open();
        MySqlCommand cmd2 = new MySqlCommand("SELECT id_czesci_symbol AS KOD,
                                                     ilosc AS ILOSC
                                              FROM `test`.`zamowienie`
                                              WHERE z_numer_naprawy='" + numberBox.Content.ToString() + "'
                                              ORDER BY ilosc LIMIT 5;", connection);
        MySqlDataAdapter adp2 = new MySqlDataAdapter(cmd2);
        DataSet ds2 = new DataSet();
        adp2.Fill(ds2, "LoadDataBinding");
        czesciTable2.DataContext = ds2;
    }
}
catch (MySqlException ex) 
{
     MessageBox.Show(ex.ToString());
}
finally 
{
     connection.Close();
}

1 个答案:

答案 0 :(得分:0)

要直接回答您的问题,请参阅下面的示例代码。

try
{
    for (int i = 0; i <= 9; i++ )
    {
        connection.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT id_czesci_symbol AS KOD,
                                                    ilosc AS ILOSC
                                             FROM `test`.`zamowienie`
                                             WHERE z_numer_naprawy='" + numberBox.Content.ToString() + "'
                                             ORDER BY ilosc LIMIT 5;", connection);
        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds, "LoadDataBinding");
        if (i < 5)
            czesciTable.DataContext = ds;
        else
            czesciTable2.DataContext = ds;

        cmd.Dispose();
        adp.Dispose();
    }
}catch (MySqlException ex) {
     MessageBox.Show(ex.ToString());
}finally {
     connection.Close();
}

您要存档的是什么?也许我们可以帮助您更好地解决问题。