DataGridView绑定製作主從表

 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.綜合實例


 暂时略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值