如何在Datagridview C#中显示更多表
本文关键字:显示 Datagridview | 更新日期: 2023-09-27 18:28:28
我可以使用c#和mysql按日期排序在一个datagridview中显示两个或多个表吗?
例如:
|table1.salesNo|table1.salesMoney|table1.date1|
|表2.purchNo|table2.purchMoney|table2.date2|
|表2.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;
}
感谢
据我所知,DataBind()仅适用于Web窗体。尝试仅绑定属性.DataSource。万一仍然不起作用,请尝试.Refresh()
dataGridView1.DataSource = jedataset;
dataGridView1.Refresh();
编辑:如果你仍然没有看到任何数据添加:
dataGridView1.AutoGenerateColumns = true
(我不知道你是否已经添加了列)
编辑2:在这里找到了这个代码,我没有测试过,但你可以调整它并尝试是否有效:
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");
}