How to update my data bound control?

G

Guest

This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but here I
am in fornt of my PC and well, the books are all the way over the other side
of the room, and my google searching technique rather leaves something to be
desired.

So, I wrote myself an interface

public interface IPerson { string Name { get; set; } }

and I wrote an implementation or two

public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name = value; } }
}

Then I got around to writing a controller

public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}

And then I wrote a view (aka a User control with a textbox on it)

public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}

then I thought that I should place my CustomerView onto a windows form with
a couple of buttons to loop through my List<IPerson> of people (forwards and
back)

public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;

public Ui()
{
InitializeComponent();

_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}

private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}

private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}

private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}

private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}

No problems here, it all works as one would like and expect.

So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean look
in particular at the following code

::In the form containing my UserControl

_ctrlCustomer.Update(_list[_position]);

::In the UserControl's Update method

_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));

Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.

I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost of
references hanging around eating away at my meagre amount of RAM.?

So here I am trying to see what others might say on the matter.
 
S

Stephany Young

If I understand your problem correctly, it is that you would rather ask
other people to do your research for your than get off your backside and
walk across your room?


billr said:
This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but
here I
am in fornt of my PC and well, the books are all the way over the other
side
of the room, and my google searching technique rather leaves something to
be
desired.

So, I wrote myself an interface

public interface IPerson { string Name { get; set; } }

and I wrote an implementation or two

public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}

Then I got around to writing a controller

public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}

And then I wrote a view (aka a User control with a textbox on it)

public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}

then I thought that I should place my CustomerView onto a windows form
with
a couple of buttons to loop through my List<IPerson> of people (forwards
and
back)

public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;

public Ui()
{
InitializeComponent();

_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}

private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}

private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}

private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}

private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}

No problems here, it all works as one would like and expect.

So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean
look
in particular at the following code

::In the form containing my UserControl

_ctrlCustomer.Update(_list[_position]);

::In the UserControl's Update method

_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));

Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.

I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost
of
references hanging around eating away at my meagre amount of RAM.?

So here I am trying to see what others might say on the matter.

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
 
B

Bob Powell [MVP]

The cardinal rule of data binding is that the data objects should implement
change notification, either by INotifyPropertyChanged or by providing a
<property>Changed event for each property that takes part in the binding.

Rather that implement your own collections you should use a
BindingList<IPerson>. This implements the IBindingList interface which
enables the currency manager to select the correct item.

Once you have a list you can use a BindingNavigator to select the objects in
the list.

Remember also that you can add your list as a data source in Visual Studio
and then use DragOnce to create a data interface for it. If you're _really_
lazy this is of course the best bet.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


billr said:
This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but
here I
am in fornt of my PC and well, the books are all the way over the other
side
of the room, and my google searching technique rather leaves something to
be
desired.

So, I wrote myself an interface

public interface IPerson { string Name { get; set; } }

and I wrote an implementation or two

public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}

Then I got around to writing a controller

public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}

And then I wrote a view (aka a User control with a textbox on it)

public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}

then I thought that I should place my CustomerView onto a windows form
with
a couple of buttons to loop through my List<IPerson> of people (forwards
and
back)

public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;

public Ui()
{
InitializeComponent();

_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}

private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}

private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}

private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}

private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}

No problems here, it all works as one would like and expect.

So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean
look
in particular at the following code

::In the form containing my UserControl

_ctrlCustomer.Update(_list[_position]);

::In the UserControl's Update method

_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));

Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.

I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost
of
references hanging around eating away at my meagre amount of RAM.?

So here I am trying to see what others might say on the matter.

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
 
G

Guest

Sorry Stephany, it seems that you do not understand my problem at all.

The point of my writing the question was to see if any professional (which
it appears is a group within which you cannot be found) who has addressed
this issue might be able to say to me .. "hey you're going about it the wrong
way" or perhaps ... "yep, that is the way to do it", or maybe even "there are
some really good resources at http://suchandsuchaurl"

if my "aparent laziness" offends you so much, you should not feel the need
to reply, clearly your response is not in any way the kind of response anyone
would be looking for, as it is neither helpful nor (most deffinately) is it
insightful.

However, I thank you for taking the time to be so helpless :)

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk


Stephany Young said:
If I understand your problem correctly, it is that you would rather ask
other people to do your research for your than get off your backside and
walk across your room?


billr said:
This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but
here I
am in fornt of my PC and well, the books are all the way over the other
side
of the room, and my google searching technique rather leaves something to
be
desired.

So, I wrote myself an interface

public interface IPerson { string Name { get; set; } }

and I wrote an implementation or two

public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}

Then I got around to writing a controller

public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}

And then I wrote a view (aka a User control with a textbox on it)

public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}

then I thought that I should place my CustomerView onto a windows form
with
a couple of buttons to loop through my List<IPerson> of people (forwards
and
back)

public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;

public Ui()
{
InitializeComponent();

_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}

private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}

private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}

private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}

private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}

No problems here, it all works as one would like and expect.

So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean
look
in particular at the following code

::In the form containing my UserControl

_ctrlCustomer.Update(_list[_position]);

::In the UserControl's Update method

_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));

Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.

I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost
of
references hanging around eating away at my meagre amount of RAM.?

So here I am trying to see what others might say on the matter.

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
 
G

Guest

Thanks Bob,

this information is very useful, and his inspired me to pick up yet another
book to investigate further (now I know what I should be looking for).

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk


Bob Powell said:
The cardinal rule of data binding is that the data objects should implement
change notification, either by INotifyPropertyChanged or by providing a
<property>Changed event for each property that takes part in the binding.

Rather that implement your own collections you should use a
BindingList<IPerson>. This implements the IBindingList interface which
enables the currency manager to select the correct item.

Once you have a list you can use a BindingNavigator to select the objects in
the list.

Remember also that you can add your list as a data source in Visual Studio
and then use DragOnce to create a data interface for it. If you're _really_
lazy this is of course the best bet.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


billr said:
This sounds like a real stoopid question I know, and it would most likely
take me less to to do a bit of reading than to write the question, but
here I
am in fornt of my PC and well, the books are all the way over the other
side
of the room, and my google searching technique rather leaves something to
be
desired.

So, I wrote myself an interface

public interface IPerson { string Name { get; set; } }

and I wrote an implementation or two

public class Man : IPerson
{
public Man(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}
public class Woman : IPerson
{
public Woman(string name) { Name = name; }
private string _name = string.Empty;
public string Name { get { return (_name); } set { _name =
value; } }
}

Then I got around to writing a controller

public class PersonController
{
public event EventHandler NameChanged;
private IPerson _person = null;
public PersonController(IPerson p) { _person = p; }
public string Name
{
get { return (_person.Name); }
set
{
_person.Name = value;
if (NameChanged != null)
NameChanged(this, EventArgs.Empty);
}
}
public void Update(IPerson p) { _person = p; }
}

And then I wrote a view (aka a User control with a textbox on it)

public partial class CustomerView : UserControl
{
private PersonController _personController = null;
public CustomerView() { InitializeComponent(); }
public void Update(IPerson p)
{
_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));
}
}

then I thought that I should place my CustomerView onto a windows form
with
a couple of buttons to loop through my List<IPerson> of people (forwards
and
back)

public partial class Ui : Form
{
private List<IPerson> _list = new List<IPerson>();
private int _position = -1;

public Ui()
{
InitializeComponent();

_list.Add(new Man("Dave"));
_list.Add(new Woman("Sally"));
_list.Add(new Man("Geoff"));
_list.Add(new Woman("Nicki"));
AdvanceIterator();
}

private void AdvanceIterator()
{
if (++_position == _list.Count)
_position = 0;
_ctrlCustomer.Update(_list[_position]);
}

private void RetardIterator()
{
if (--_position == -1)
_position += _list.Count;
_ctrlCustomer.Update(_list[_position]);
}

private void _btnPrevious_Click(object sender, EventArgs e)
{
RetardIterator();
}

private void _btnNext_Click(object sender, EventArgs e)
{
AdvanceIterator();
}
}

No problems here, it all works as one would like and expect.

So it works at least. Yes, it compiles, yes it runs, yes the Model is
updated when the Controller receives an update from the View, and it's all
great .... but is this the best way to achieve the required end? I mean
look
in particular at the following code

::In the form containing my UserControl

_ctrlCustomer.Update(_list[_position]);

::In the UserControl's Update method

_txName.DataBindings.Clear();
_personController = new PersonController(p);
_txName.DataBindings.Add(new Binding("Text", _personController,
"Name"));

Originally I attempted to simply update the UserControl's reference to
IPerson, but the display was not updated, and one boviously does not wnat
multiple items in a collection bound to the same control.

I'm wondering should I be binding a new Controller for each call to Update
instead of clearing the existing bindings? But then won't that leave lost
of
references hanging around eating away at my meagre amount of RAM.?

So here I am trying to see what others might say on the matter.

--
Of all words of tongue and pen, the saddest are: "It might have been"

Bill.Richards @ greyskin .co .uk
http://greyskin.co.uk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top