private void Form1_Load ( object sender, EventArgs e )
{
string comm_Supplier = "select * from supplier";
string comm_products = "select * from products";
MySqlConnection conn = clsdb.getcon();
try
{
// 是否載入資料庫的關連
//da_Supplier.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da_Supplier.SelectCommand = new MySqlCommand(comm_Supplier, conn);
da_Supplier.Fill(ds, "Supplier");
da_Products.SelectCommand = new MySqlCommand(comm_products, conn);
da_Products.Fill(ds, "Products");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// 設定DataGridView資料來源
bs_Supplier.DataSource = ds;
bs_Supplier.DataMember = "Supplier";
dgv_supplier.DataSource = bs_Supplier;
// 初始化DataGridView
InitializeDataGridView(dgv_supplier);
bindingNavigator1.BindingSource = bs_Supplier;
// DataTable建立關連
DataColumn x = ds.Tables["Supplier"].Columns["sup_Num"];
DataColumn y = ds.Tables["Products"].Columns["sup_Num"];
rel = new DataRelation("pro", x, y, false);
ds.Relations.Add(rel);
// 設定DataGridView資料來源
bs_Products.DataSource = bs_Supplier;
bs_Products.DataMember = "pro";
dgv_products.DataSource = bs_Products;
}
參考資料
1:DataRelation 類別
沒有留言:
張貼留言