By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,586 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

Binding Manually for Sanity's sake - How to do it intelligently

P: n/a
Hello:

We are working on a large number of forms. The original approach was
to use the built-in data designer, type-specific DataTables and form
binding. However, we are running into various issues due to the
complexity of our forms. For instance, how do you bind to a check box
with a data table, or a radio group?

Well, I have since started making business objects that wrap around
DataRows. The business objects simply provide properties that wrap
around the DataRow data and return it in a desired format.

To maintain all my business objects, I am creating a
BindingList<MyBO>. Doing this really is frustrating since it means I
have to handle the BindingList<MyBO>.AddingNew event. Also, when I am
navigating with a DataNavigator control, I have to manually rebind all
my controls.

So I am holding a reference to a DataTable, BindingList<MyBOand a
BindingSource that controls the BindingList. It is really confusing.
It can't be this confusing to use business objects in Windows Forms.
What am I missing that will help simplify this new approach?

Thanks for any pointers,
Travis

Nov 26 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Well, you can use automatic binding for most properties (including
check-box). The radio is an exception perhaps. Is there a specific
binding issue you are trying to avoid?

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;

public class Person : INotifyPropertyChanged
{
private string name;
private DateTime dateOfBirth = DateTime.Today;
private bool isActive;
public string Name {
get {return name;}
set {SetField(ref name, value, "Name");}
}
public bool IsActive
{
get { return isActive; }
set { SetField(ref isActive, value, "IsActive"); }
}
public DateTime DateOfBirth {
get {return dateOfBirth;}
set {SetField(ref dateOfBirth, value, "DateOfBirth");}
}
public Person() { }
public Person(string name, DateTime dateOfBirth, bool isActive) {
Name = name; DateOfBirth = dateOfBirth; IsActive = isActive;
}
protected void SetField<T>(ref T field, T value, string
propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{ // changed
field = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
static class Program
{

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
BindingList<Personpeople = new BindingList<Person>();
people.Add(new Person("Fred",
DateTime.Today.AddYears(-20),true));
people.Add(new Person("Wilma",
DateTime.Today.AddYears(-25),false));
BindingSource bs = new BindingSource(people, "");
bs.DataError += delegate { };
using (Form f = new Form())
{
DataGridView dgv = new DataGridView();
dgv.DataSource = bs;
CheckBox cb = new CheckBox();
cb.DataBindings.Add("Checked", bs, "IsActive");
TextBox tb = new TextBox();
tb.DataBindings.Add("Text", bs, "Name");
DateTimePicker dtp = new DateTimePicker();
dtp.DataBindings.Add("Value", bs, "DateOfBirth");

cb.Dock = tb.Dock = dtp.Dock = dgv.Dock = DockStyle.Top;
f.Controls.AddRange(new Control[] { cb, tb, dtp, dgv });
Application.Run(f);

}

}
}
Nov 26 '07 #2

P: n/a
On Nov 26, 1:58 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Well, you can use automatic binding for most properties (including
check-box). The radio is an exception perhaps. Is there a specific
binding issue you are trying to avoid?

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;

public class Person : INotifyPropertyChanged
{
private string name;
private DateTime dateOfBirth = DateTime.Today;
private bool isActive;
public string Name {
get {return name;}
set {SetField(ref name, value, "Name");}
}
public bool IsActive
{
get { return isActive; }
set { SetField(ref isActive, value, "IsActive"); }
}
public DateTime DateOfBirth {
get {return dateOfBirth;}
set {SetField(ref dateOfBirth, value, "DateOfBirth");}
}
public Person() { }
public Person(string name, DateTime dateOfBirth, bool isActive) {
Name = name; DateOfBirth = dateOfBirth; IsActive = isActive;
}
protected void SetField<T>(ref T field, T value, string
propertyName)
{
if (!EqualityComparer<T>.Default.Equals(field, value))
{ // changed
field = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;}

static class Program
{

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
BindingList<Personpeople = new BindingList<Person>();
people.Add(new Person("Fred",
DateTime.Today.AddYears(-20),true));
people.Add(new Person("Wilma",
DateTime.Today.AddYears(-25),false));
BindingSource bs = new BindingSource(people, "");
bs.DataError += delegate { };
using (Form f = new Form())
{
DataGridView dgv = new DataGridView();
dgv.DataSource = bs;
CheckBox cb = new CheckBox();
cb.DataBindings.Add("Checked", bs, "IsActive");
TextBox tb = new TextBox();
tb.DataBindings.Add("Text", bs, "Name");
DateTimePicker dtp = new DateTimePicker();
dtp.DataBindings.Add("Value", bs, "DateOfBirth");

cb.Dock = tb.Dock = dtp.Dock = dgv.Dock = DockStyle.Top;
f.Controls.AddRange(new Control[] { cb, tb, dtp, dgv });
Application.Run(f);

}

}

}- Hide quoted text -

- Show quoted text -
Our problem is when we move over a BindingList. it seems to not
repopulate the textbox when moving to the next record. It is like it
is only bound to a single record.
Nov 27 '07 #3

P: n/a
Our problem is when we move over a BindingList.

That sounds like a currency issue. Have you bound the different
controls to the same view of the data? This is necessary for them to
share a CurrencyManager. My example above demonstrates this working
just fine, updating the Name textbox (in my case I've wrapped with a
BindingSource for convenience) - so what is happening differently? How
(to which source objects) are you doing your various bindings?

Marc
Nov 27 '07 #4

P: n/a
Note that databinding is not type safe.
...NOT for the strings you use for databinds.
Very, very true. Oh, for a very simple solution that has been
discussed here (to death) before; a new
"nameof(SomeType.SomeAccessibleMember)" compile-time operator, which
might evaulate to a string but get checked at compile-time. Presumably
it would be very easy to implement, and invaluable to data-binding.

Oh well, we live with what we have...

Marc
Nov 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.