DataGridView绑定製作主從表
1.确保绑定到同一数据源的多个控件保持同步
bindingSource1.BindingComplete +=
new BindingCompleteEventHandler(bindingSource1_BindingComplete);
private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
// Check if the data source has been updated, and that no error has occured.
if (e.BindingCompleteContext ==
BindingCompleteContext.DataSourceUpdate && e.Exception == null)
// If not, end the current edit.
e.Binding.BindingManagerBase.EndCurrentEdit();
}
2.使Binding後的TextBox可失去焦點
masterBindingSource.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete);
private void bindingSource1_BindingComplete(object sender, BindingCompleteEventArgs e)
{
if (e.Exception != null)
{
e.Cancel = false;//這一句將使textbox中的值與table[head]中相應欄位的值不一致.
return;
}
if (e.BindingCompleteContext == BindingCompleteContext.DataSourceUpdate && e.Exception == null)
e.Binding.BindingManagerBase.EndCurrentEdit();
}
3.CurrencyManager的使用,TextBox中數據可自動與DataGridView同步
private BindingSource masterBindingSource;
private DataSet ds = new DataSet();
private CurrencyManager currency;
private void BindingFrom_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("server=(local);database=testdb;uid=sa;pwd=123456"))
{
SqlCommand cmd = new SqlCommand("select d1,d2,d3 from head where 1=2");
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "head");
}
masterBindingSource = new BindingSource(this.ds, "head");
dataGridView1.DataSource = masterBindingSource;
this.currency = (CurrencyManager)this.dataGridView1.BindingContext[this.ds, "head"];
masterBindingSource.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete);
//
this.dataGridView1.Columns["d1"].HeaderText = "d1Col";
this.dataGridView1.Columns["d2"].HeaderText = "d2Col";
//
textBox1.DataBindings.Add("Text", masterBindingSource, "d1", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", masterBindingSource, "d2", true, DataSourceUpdateMode.OnPropertyChanged);
textBox3.DataBindings.Add("Text", masterBindingSource, "d3", true, DataSourceUpdateMode.OnPropertyChanged);
//
}
4.從表隨主表變動
private BindingSource detailBindingSource;
private DataSet ds = new DataSet();
this.dataGridView1.CurrentCellChanged += new EventHandler(dataGridView1_CurrentCellChanged);
void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
if (this.dataGridView1.CurrentCell != null)
{
InitDetail(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells["d1"].Value.ToString());
}
else
{
InitDetail((string)null);
}
}
private void InitDetail(string keyValue)
{
if (this.detailBindingSource != null)
{
this.ds.Tables["body"].Clear();
}
if (keyValue == (string)null)
{
return;
}
using (SqlConnection conn = new SqlConnection("server=(local);database=testdb;uid=sa;pwd=123456"))
{
SqlCommand cmdDetail = new SqlCommand("select d2,d3 from body where d1='"+keyValue+"'");
cmdDetail.Connection = conn;
SqlDataAdapter daDetail = new SqlDataAdapter(cmdDetail);
daDetail.Fill(ds, "body");
}
detailBindingSource = new BindingSource(this.ds, "body");
dataGridView2.DataSource = detailBindingSource;
}
5.主表變動
private BindingSource masterBindingSource;
private DataSet ds = new DataSet();
private void InitMaster(string field1, string field2)
{
this.ds.Tables["head"].Rows.Clear();
using (SqlConnection conn = new SqlConnection("server=(local);database=testdb;uid=sa;pwd=19821026"))
{
SqlCommand cmd = new SqlCommand("select d1,d2,d3 from head where d1 like '%" + field1 + "%' and d2 like '%"+field2+"%'");
cmd.Connection = conn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "head");
}
masterBindingSource = new BindingSource(this.ds, "head");
dataGridView1.DataSource = masterBindingSource;
this.currency = (CurrencyManager)this.dataGridView1.BindingContext[this.ds, "head"];
//使數據實時同步改變
masterBindingSource.BindingComplete += new BindingCompleteEventHandler(bindingSource1_BindingComplete);
ClearBinding();
BindingHead();
}
private void ClearBinding()
{
this.textBox1.DataBindings.Clear();
this.textBox2.DataBindings.Clear();
this.textBox3.DataBindings.Clear();
}
private void BindingHead()
{
//bind TextBox
textBox1.DataBindings.Add("Text", masterBindingSource, "d1", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", masterBindingSource, "d2", true, DataSourceUpdateMode.OnPropertyChanged);
textBox3.DataBindings.Add("Text", masterBindingSource, "d3", true, DataSourceUpdateMode.OnPropertyChanged);
//
}
6.綜合實例
暂时略
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/manimanihome/archive/2008/07/18/2673325.aspx