BindingSource应用两例

BindingSource应用两例

 

一、BindingSource应用(自定义属性)

public partial class Form15 : Form

    {

        /*

         * BindingSource应用(自定义属性)

         */

        public Form15()

        {

            InitializeComponent();

 

            //实例化BindingSource

            bs = new BindingSource();

 

            //建立List

            List<TestItem> lstItem = new List<TestItem>();

            TestItem item = new TestItem();

            item.ItemName = "A";

            lstItem.Add(item);

 

            item = new TestItem();

            item.ItemName = "B";

            lstItem.Add(item);

 

            //绑字bs数据源

            bs.DataSource = lstItem;          

 

            //建立textBox1,lable1DataBinding

            textBox1.DataBindings.Add(new Binding("Text", bs, "ItemName"));

            label1.DataBindings.Add(new Binding("Text", bs, "ItemName"));

        }

 

        BindingSource bs = null;

 

        //单击按钮移动bsCurrent

        private void button1_Click(object sender, EventArgs e)

        {

 

            bs.MoveNext();

        }

    }

 

    public class TestItem

    {

        public string ItemName

        {

            get;

            set;

        }

    }

 

 

二、BindingSource应用(DataSet/DataTable)

public partial class Form16 : Form

    {

 

        /*

         *BindingSource应用(DataSet/DataTable)

         *

         */

        public Form16()

        {

            InitializeComponent();

 

            //建立DataSet(dt,dt2)

            DataSet ds = new DataSet();

            DataTable dt = new DataTable();

            dt.TableName = "T1";

            dt.Columns.Add("Col1", typeof(int));

            dt.Columns.Add("Col2", typeof(string));

 

            DataRow drNew = dt.NewRow();

            drNew["Col1"] = 1;

            drNew["Col2"] = "AAA";

            dt.Rows.Add(drNew);

 

            drNew = dt.NewRow();

            drNew["Col1"] = 2;

            drNew["Col2"] = "BBB";

            dt.Rows.Add(drNew);

 

            DataTable dt2 = new DataTable();

            dt2.TableName = "T2";

            dt2.Columns.Add("Col1", typeof(int));

            dt2.Columns.Add("Col2", typeof(string));

 

            drNew = dt2.NewRow();

            drNew["Col1"] = 1;

            drNew["Col2"] = "AAA";

            dt2.Rows.Add(drNew);

 

            drNew = dt2.NewRow();

            drNew["Col1"] = 1;

            drNew["Col2"] = "AAB";

            dt2.Rows.Add(drNew);

 

            drNew = dt2.NewRow();

            drNew["Col1"] = 2;

            drNew["Col2"] = "BBB";

            dt2.Rows.Add(drNew);

 

            drNew = dt2.NewRow();

            drNew["Col1"] = 2;

            drNew["Col2"] = "BBC";

            dt2.Rows.Add(drNew);

 

            ds.Tables.AddRange(new DataTable[] { dt, dt2 });

 

            //bs2会受bsCurrent切换而改动,故先绑定bs2

            BindingSource bs2 = new BindingSource();

            bs2.DataSource = ds.Tables["T2"];

            gridControl2.DataSource = bs2;

 

            BindingSource bs = new BindingSource();

            //bs挂勾事件,切换行事件

            bs.CurrentItemChanged += new EventHandler(bs_CurrentItemChanged);

           

            bs.DataSource = ds.Tables["T1"];

 

            gridControl1.DataSource = bs;

 

        }

 

        //bs切换行事件,设定bs2Filter,从而刷新gridControl2的数据显示

        void bs_CurrentItemChanged(object sender, EventArgs e)

        {

            (gridControl2.DataSource as BindingSource).Filter = "Col1=" + ((sender as BindingSource).Current as DataRowView).Row ["Col1"].ToString();

        }

    }

你可能感兴趣的:(BindingSource应用两例)