如何使用 BindingSource 绑定 ListBox,同时解决绑定 List 后修改数据源不能同时刷新界面显示的问题
View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace X.BindingSourceDemo
{
    /// <summary>
    
/// 示例:如何使用 BindingSource 绑定 ListBox,同时解决绑定 List<T> 后修改数据源不能同时刷新界面显示的问题
    
/// 描述:1 下面的示例分别以 List<T>, BindingList<T> 及 DataTable 来绑定 ListBox
    
///        2 以 List<T> 作为数据源时直接对数据的修改界面不能刷新的问题可以通过以下三种方式来解决
    
///          a> 更新完数据后调用 BindingSource 的 ResetBindings
    
///          b> 通过 BindingSource 自带的方法来更新数据源
    
///          c> 使用 BindingList<T> 或 DataTable 对象来代替 List<T>
    
/// 日期:2009-09-9
    
/// </summary>
    public partial class Demo : Form
    {
        public class Employee
        {
            public Int32 Id { getset; }
            public String Name { getset; }
            public Employee(Int32 Id, String Name)
            {
                this.Id = Id;
                this.Name = Name;
            }
        }

        private BindingSource bs1;
        private BindingSource bs2;
        private BindingSource bs3;
        
        private List<Employee> listSource;
        private BindingList<Employee> bindingListSource;
        private DataTable dataTableSource;

        private Int32 index = 0;

        private ListBox listBox1;
        private ListBox listBox2;
        private ListBox listBox3;
        private Button btnAdd;

        public Demo()
        {
            InitializeComponent();
            this.InitClass();
        }

        private void InitClass()
        {
            #region " 初始数据源 "

            this.listSource = new List<Employee>();
            this.bindingListSource = new BindingList<Employee>();

            this.dataTableSource = new DataTable();
            this.dataTableSource.Columns.Add("Id"typeof(Int32));
            this.dataTableSource.Columns.Add("Name"typeof(String));

            this.bs1 = new BindingSource(this.listSource, null);
            this.bs2 = new BindingSource(this.bindingListSource, null);
            this.bs3 = new BindingSource(this.dataTableSource, null);

            for (index = 0; index < 5; index++)
            {
                this.listSource.Add(new Employee(index, String.Format("Name of {0}", index)));
                this.bindingListSource.Add(new Employee(index, String.Format("Name of {0}", index)));
                this.dataTableSource.Rows.Add(new object[] { index, String.Format("Name of {0}", index) });
            }

            #endregion

            #region " 初始控件 "

            this.listBox1 = new ListBox();
            this.listBox1.Location = new Point(1012);

            this.listBox2 = new ListBox();
            this.listBox2.Location = new Point(15012);

            this.listBox3 = new ListBox();
            this.listBox3.Location = new Point(31012);

            this.btnAdd = new Button();
            this.btnAdd.Location = new Point(10120);
            this.btnAdd.Text = "添加";
            this.btnAdd.Click += new EventHandler(btnAdd_Click);

            this.listBox1.DataSource = this.bs1;
            this.listBox2.DataSource = this.bs2;
            this.listBox3.DataSource = this.bs3;

            this.listBox1.DisplayMember = "Name";
            this.listBox1.ValueMember = "Id";

            this.listBox2.DisplayMember = "Name";
            this.listBox2.ValueMember = "Id";

            this.listBox3.DisplayMember = "Name";
            this.listBox3.ValueMember = "Id";

            this.Controls.AddRange(new Control[] { this.listBox1, this.listBox2, this.listBox3, this.btnAdd });

            this.Size = new Size(500500);
            #endregion
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            // 分别向三个数据源添加数据

            
//#1 数据源对象 List<T>
            
//方法一:当数据源发生改变时调用 ResetBindings
            
//this.listSource.Add(new Employee(index, String.Format("Name of {0}", index)));
            
//this.bs1.ResetBindings(true);

            
//方法二:使用 BindingSource 自带的方法来更新数据源
            this.bs1.Add(new Employee(index, String.Format("Name of {0}", index))); //添加数据
            
//this.bs1[0] = new Employee(1, "JJJ"); // 修改数据
            
//this.bs1.RemoveAt(0); // 删除数据

            
//#2 数据源对象 BindingList<T>
            this.bindingListSource.Add(new Employee(index, String.Format("Name of {0}", index)));

            //#3 数据源对象 DataTable
            this.dataTableSource.Rows.Add(new object[] { index, String.Format("Name of {0}", index) });
            this.index++;
        }
    }
}
posted on 2012-04-17 17:08  Wgms  阅读(913)  评论(0)    收藏  举报