如何在Datagridview中显示更多表C#

时间:2015-12-28 23:57:04

标签: c# mysql

我可以使用c#和mysql按日期排序在一个datagridview中显示两个或更多表吗?

代表:

| table1.salesNo | table1.salesMoney | table1.date1 |

| table2.purchNo | table2.purchMoney | table2.date2 |

| table2.purchNo | table2.purchMoney | table2.date3 |

| table1.salesNo | table1.salesMoney | table1.date4 |

| table1.salesNo | table1.salesMoney | table1.date5 |

我使用此代码但没有数据出现

 private MySqlDataAdapter salesinvoices, purchasesinvoices;
        private DataSet jedataset;
private void button2_Click(object sender, EventArgs e)
      {
          const string SELECT_salesinvoices = "SELECT * FROM sales_invoices";
          const string SELECT_purchasesinvoices = "SELECT * FROM purchase_invoices";



          // Compose the connection string.
          string connect_string = Publics.je_Coonn;

          // Create a DataAdapter to load the Addresses table.
          salesinvoices = new MySqlDataAdapter(SELECT_salesinvoices,
              connect_string);

          // Create a DataAdapter to load the Addresses table.
          purchasesinvoices = new MySqlDataAdapter(SELECT_purchasesinvoices,
              connect_string);

          // Create and fill the DataSet.
          jedataset = new DataSet("je_coronasalesdbDataSet");
          salesinvoices.Fill(jedataset, "sales_invoices");
          purchasesinvoices.Fill(jedataset, "purchase_invoices");

          // Bind the DataGrid to the DataSet.
          dataGridView1.DataSource = jedataset;
      }

感谢

1 个答案:

答案 0 :(得分:1)

据我所知, DataBind()仅适用于Web窗体。尝试使用 .DataSource 属性进行绑定。如果仍然无法正常工作,请尝试 .Refresh()

dataGridView1.DataSource = jedataset;
dataGridView1.Refresh();

编辑:如果您仍然看不到数据添加:

dataGridView1.AutoGenerateColumns = true

(我不知道你是否已添加了这些列)

编辑2:找到此代码here,我没有对其进行测试,但您可以对其进行调整并尝试运行:

private static void DemonstrateMergeTable()
{
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };



    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the  
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}