Should I be updating the data source or the control?
-
Wednesday, August 22, 2012 12:16 AM
I'm working in WinForms C#.NET
I have a form that I manually created the databindings for like so: txtDueDate.DataBindings.Add(new Binding("Text", bindingSource, "DueDate", true, DataSourceUpdateMode.OnPropertyChanged));
My question is, to update my form (UI and underlying datasource) should I be updating the underlying data source ((BusinessObject)(bindingSource.Current).DueDate = 12-12-12), or should I be updating the control value (txtDueDate.Text = 12-12-12)?
I don't seem to be getting consistent results with some of my controls (especially combo boxes), so I was wondering which is the correct way to do this.
Edit:
I think I figured it out. In case anyone else is looking for this, update the control, the form dataSource gets updated "OnPropertyChanged". So whenever you update the control, it fires back into the data source.
- Edited by Danda1man Wednesday, August 22, 2012 1:45 AM Figured It out
All Replies
-
Wednesday, August 22, 2012 7:14 AMModerator
I think I figured it out. In case anyone else is looking for this, update the control, the form dataSource gets updated "OnPropertyChanged". So whenever you update the control, it fires back into the data source.
I'm glad you figure it out. Base on my understanding, you can also update the control value through update underlying code. For example,
public partial class Form1 : Form { public Form1() { InitializeComponent(); } DataTable dt = new DataTable(); BindingSource bindingSource = new BindingSource(); private void Form1_Load(object sender, EventArgs e) { dt.Columns.Add("DueDate"); dt.Rows.Add("a"); bindingSource.DataSource = dt; this.textBox1.DataBindings.Add((new Binding("Text", bindingSource, "DueDate", true, DataSourceUpdateMode.OnPropertyChanged))); } private void button1_Click(object sender, EventArgs e) { DataRowView drv = bindingSource.Current as DataRowView; drv.Row[0] = "new"; } }Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Bob Wu-MTMicrosoft Contingent Staff, Moderator Friday, September 07, 2012 5:42 AM


