c# Winform同一數據源多個控件保持同步
在瞭解Windows窗體數據綁定直接,對控件數據綁定一般都是直接對控件可接收值的屬性賦值,如文本框的Text屬性,但在做一些列表數據處理時,同一份數據要多處處理,對各個控件的屬性逐一賦值顯得比較麻煩。通過對控件屬性設置數據源綁定,利用Windows數據更改通知這一特性,隻要訂閱(設定綁定)的控件都能接收到數據的變化通知。
通過DataBindings方法實現雙向數據綁定
textBox1.DataBindings.Add("Text", trackBar1, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
實現 INotifyPropertyChanged 接口,當屬性值更改時引發事件通知
private Serial serial; public Form3() { InitializeComponent(); serial = new Serial(); this.textBox1.DataBindings.Add("Text", serial, "Number", true, DataSourceUpdateMode.OnPropertyChanged); } private void button1_Click(object sender, EventArgs e) { serial.Number += 1; } public class Serial : INotifyPropertyChanged { private int number; public int Number { get { return number; } set { if (value != number) { number = value; NotifyPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
BindingList實現瞭雙向Windows窗體數據綁定所需的主要接口。通過將BindingSource數據源綁定到控件,可以使多個控件保持數據同步
public partial class Form1 : Form { private IList<Foo> foos; private BindingList<Foo> bindingList; public Form1() { InitializeComponent(); dataGridView1.MultiSelect = false; // 數據源 foos = new List<Foo> { new Foo(){Id=Guid.NewGuid().ToString(), Field1="張三",Field2="選項一",Field3=DateTime.Now.AddYears(-6),Field4="1918731555X",Field5="雲南省昆明市...街道66號"}, new Foo(){Id=Guid.NewGuid().ToString(), Field1="李四",Field2="選項二",Field3=DateTime.Now.AddYears(-7),Field4="1918731556X",Field5="雲南省玉溪市...街道56號"}, new Foo(){Id=Guid.NewGuid().ToString(), Field1="王五",Field2="選項一",Field3=DateTime.Now.AddYears(-8),Field4="1918731557X",Field5="雲南省紅河州...街道46號"}, new Foo(){Id=Guid.NewGuid().ToString(), Field1="趙六",Field2="選項三",Field3=DateTime.Now.AddYears(-9),Field4="1918731558X",Field5="雲南省楚雄州...街道36號"}, }; this.Load += Form1_Load; } private void Form1_Load(object sender, EventArgs e) { bindingList = new BindingList<Foo>(foos); var source = new BindingSource(bindingList, null); // 控件數據綁定 dataGridView1.DataSource = source; textBox1.DataBindings.Add("Text", source, "Field1", true, DataSourceUpdateMode.OnPropertyChanged); comboBox1.DataBindings.Add("Text", source, "Field2", true, DataSourceUpdateMode.OnPropertyChanged); dateTimePicker1.DataBindings.Add("Value", source, "Field3", true, DataSourceUpdateMode.OnPropertyChanged); textBox2.DataBindings.Add("Text", source, "Field4", true, DataSourceUpdateMode.OnPropertyChanged); textBox3.DataBindings.Add("Text", source, "Field5", true, DataSourceUpdateMode.OnPropertyChanged); bindingList.AddingNew += BindingList_AddingNew; } private void BindingList_AddingNew(object sender, AddingNewEventArgs e) { e.NewObject = new Foo() { Id = Guid.NewGuid().ToString() }; } // 保存 private void button1_Click(object sender, EventArgs e) { } // 新增 private void button2_Click(object sender, EventArgs e) { // 方式一 //bindingList.Add(new Foo() { Id = Guid.NewGuid().ToString() }); //int selectIndex = dataGridView1.Rows.Count - 1; //dataGridView1.Rows[selectIndex].Selected = true; //dataGridView1.CurrentCell = dataGridView1.Rows[selectIndex].Cells[0]; //方式二 var foo = bindingList.AddNew(); } // 刪除 private void button3_Click(object sender, EventArgs e) { bindingList.RemoveAt(dataGridView1.CurrentRow.Index); dataGridView1.Rows[0].Selected = true; dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0]; } } class Foo { public string Id { get; set; } public string Field1 { get; set; } public string Field2 { get; set; } public DateTime Field3 { get; set; } public string Field4 { get; set; } public string Field5 { get; set; } }
以上就是c# Winform同一數據源多個控件保持同步的詳細內容,更多關於Winform同一數據源多個控件保持同步的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- Winform項目中TextBox控件DataBindings屬性
- C#實現學生模塊的增刪改查
- C#開發WinForm根據條件改變DataGridView行顏色
- C#開發WinForm清空DataGridView控件綁定的數據
- C#數據表格(DataGridView)控件的應用案例