WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

WinForm控件复杂数据绑定常用数据源(对Combobox,DataGridView等控件DataSource赋值的多种方法)

开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定。

1) 简单数据绑定

简单的数据绑定是将用户控件的某一个属性绑定至某一个类型实例上的某一属性。采用如下形式进行绑定:引用控件.DataBindings.Add("控件属性", 实例对象, "属性名", true);

2) 复杂数据绑定

复杂的数据绑定是将一个以列表为基础的用户控件(例如:ComboBox、ListBox、ErrorProvider、DataGridView等控件)绑定至一个数据对象的列表

基本上,Windows Forms的复杂数据绑定允许绑定至支持IList接口的数据列表。此外,如果想通过一个BindingSource组件进行绑定,还可以绑定至一个支持IEnumerable接口的数据列表。

对于复杂数据绑定,常用的数据源类型有(代码以Combobox作为示例控件):

1IList接口(包括一维数组,ArrayList等)

示例:

        private void InitialComboboxByList()

        {

            ArrayList list = new ArrayList();

            for (int i = 0; i < 10;i++ )

            {

                list.Add(new DictionaryEntry(i.ToString(), i.ToString() + "_value"));

            }

            this.comboBox1.DisplayMember = "Key";

            this.comboBox1.ValueMember = "Value";

            this.comboBox1.DataSource = list;

        }

2IListSource接口(DataTableDataSet等)

        private void InitialComboboxByDataTable()

        {

            DataTable dt=new DataTable();

            DataColumn dc1 = new DataColumn("Name");

            DataColumn dc2 = new DataColumn("Value");

 

            dt.Columns.Add(dc1);

            dt.Columns.Add(dc2);

 

            for (int i = 1; i <=10;i++ )

            {

                DataRow dr = dt.NewRow();

                dr[0] = i;

                dr[1] = i + "--" + "hello";

                dt.Rows.Add(dr);

            }

 

            this.comboBox1.DisplayMember = "Name";

            this.comboBox1.ValueMember = "Value";

            this.comboBox1.DataSource = dt;

        }

3) IBindingList接口(如BindingList类)

4) IBindingListView接口(如BindingSource类)

示例:

        private void InitialComboboxByBindingSource ()

        {

            Dictionary<string, string> dic = new BindingSource <string, string>();

            for (int i = 0; i < 10;i++ )

            {

                dic.Add(i.ToString(),i.ToString()+"_hello");

                this.comboBox1.DisplayMember = "Key";

                this.comboBox1.ValueMember = "Value";

                this.comboBox1.DataSource = new BindingSource(dic, null);

            }

        }

注意一下上面的程序,BindingSource的DataSource属性值为一个Dictionary类型的实例,如果用dic直接给Combobox赋值的话,程序会报错,因为Dictionary<>并没有实现IList或IListSource接口,但是将Dictionary<>放到BindingSource中便可以实现间接绑定。关于BindingSource更详细的信息请在该网址查看:

    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource(VS.80).aspx 

    再看下面一个BindingSource的示例:

        private void InitialComboboxByObject()

        {

            this.comboBox1.DisplayMember = "Name";

            this.comboBox1.ValueMember = "Value";

            this.comboBox1.DataSource = new BindingSource(new DictionaryEntry("Test","Test_Value"), null);

        }

     上面代码中BindingSource的Datasource是一个结构类型DictionaryEntry,同样的DictionaryEntry并不能直接赋值给Combobox的DataSource,但通过BindingSource仍然可以间接实现。

    这是因为:

BindingSource可以作为一个强类型的数据源。其数据源的类型通过以下机制之一固定:

·             使用 Add 方法可将某项添加到 BindingSource 组件中。

·             将 DataSource 属性设置为一个列表单个对象类型。(这三者并不一定要实现IList或IListSource)

这两种机制都创建一个强类型列表。BindingSource 支持由其 DataSource 和 DataMember 属性指示的简单数据绑定和复杂数据绑定。

转自:http://minmin86121.blog.163.com/blog/static/49681157201101904297/