DataBinding not working

U

Uncle Sammy

Hey all,

I am trying to use a Listbox control with two data bindings. I cannot
get the listbox to change the textboxes. Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BindingNotWorking
{
public partial class Form1 : Form
{
public class LicensePlate
{
private string plate;
private int index;
public string PlateNumber
{
get
{
return this.plate;
}
set
{
this.plate = value;
}
}
public int Index
{
get
{
return this.index;
}
set
{
this.index = value;
}
}

public LicensePlate(string _plate, int _index)
{
this.PlateNumber = _plate;
this.Index = _index;
}
}

public class Vehicle
{
private string make;
private string model;
private int year;
private int index;
public string Make
{
get
{
return this.make;
}
set
{
this.make = value;
}
}
public string Model
{
get
{
return this.model;
}
set
{
this.model = value;
}
}
public int Year
{
get
{
return this.year;
}
set
{
this.year = value;
}
}
public int Index
{
get
{
return this.index;
}
set
{
this.index = value;
}
}

public Vehicle(string _make, string _model, int _year, int
_index)
{
this.Make = _make;
this.Model = _model;
this.Year = _year;
this.Index = _index;
}
}

private LicensePlate[] plates;
private Vehicle[] vehicles;

private void InitializeArrays()
{
vehicles = new Vehicle[] {
new Vehicle("Ford", "Mustang", 1969, 1),
new Vehicle("Chevy", "Nova", 1965, 2),
new Vehicle("Dodge", "Van", 1975, 0)
};
plates = new LicensePlate[] {
new LicensePlate("YVX 809", 1),
new LicensePlate("BAD 001", 2),
new LicensePlate("3M TA3", 0)
};
}

private void BindEmAll()
{
this.listBoxPLATES.DataSource = plates;
this.listBoxPLATES.DisplayMember = "PlateNumber";
this.listBoxPLATES.ValueMember = "Index";

this.textBoxMAKE.DataBindings.Add("Text", vehicles,
"Make");
this.textBoxMODEL.DataBindings.Add("Text", vehicles,
"Model");
this.textBoxYEAR.DataBindings.Add("Text", vehicles,
"Year");

this.listBoxPLATES.DataBindings.Add("SelectedValue",
vehicles, "Index");
}

public Form1()
{
InitializeComponent();
this.InitializeArrays();
this.BindEmAll();
}

}
}
 
R

Russell Mangel

You have a few problems here.
First, you can't do this with textBoxes as they only support simple binding.
You have a relationship setup using "index". You could setup binding and
display the index in the textboxes, but then this is not what you want.
Second, if you want to use a relationship (index), then your vehicles
datasource should be bound to a dataGridView, this way you can see all the
fields in one control "Ford" "Mustang", "Year". Third, you should use a
BindingSource and BindingListView<T> for your collection classes and *not*
simple arrays, as they don't have the proper events to notify other
controls.

Windows Forms Binding is a complex topic, but very useful once you get the
hang of it. I highly recommend that you buy the following book if you
haven't purchased it:
http://www.amazon.com/gp/product/03...f=pd_bbs_1/102-2957954-2447360?_encoding=UTF8

Data Binding with Windows Forms 2.0 : Programming Smart Client Data
Applications with .NET by Brian Noyes

If you still need help, post a reply here, and I will make a sample program
that will do what you need.

Russell Mangel
Las Vegas, NV

Uncle Sammy said:
Hey all,

I am trying to use a Listbox control with two data bindings. I cannot
get the listbox to change the textboxes. Here's the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BindingNotWorking
{
public partial class Form1 : Form
{
public class LicensePlate
{
private string plate;
private int index;
public string PlateNumber
{
get
{
return this.plate;
}
set
{
this.plate = value;
}
}
public int Index
{
get
{
return this.index;
}
set
{
this.index = value;
}
}

public LicensePlate(string _plate, int _index)
{
this.PlateNumber = _plate;
this.Index = _index;
}
}

public class Vehicle
{
private string make;
private string model;
private int year;
private int index;
public string Make
{
get
{
return this.make;
}
set
{
this.make = value;
}
}
public string Model
{
get
{
return this.model;
}
set
{
this.model = value;
}
}
public int Year
{
get
{
return this.year;
}
set
{
this.year = value;
}
}
public int Index
{
get
{
return this.index;
}
set
{
this.index = value;
}
}

public Vehicle(string _make, string _model, int _year, int
_index)
{
this.Make = _make;
this.Model = _model;
this.Year = _year;
this.Index = _index;
}
}

private LicensePlate[] plates;
private Vehicle[] vehicles;

private void InitializeArrays()
{
vehicles = new Vehicle[] {
new Vehicle("Ford", "Mustang", 1969, 1),
new Vehicle("Chevy", "Nova", 1965, 2),
new Vehicle("Dodge", "Van", 1975, 0)
};
plates = new LicensePlate[] {
new LicensePlate("YVX 809", 1),
new LicensePlate("BAD 001", 2),
new LicensePlate("3M TA3", 0)
};
}

private void BindEmAll()
{
this.listBoxPLATES.DataSource = plates;
this.listBoxPLATES.DisplayMember = "PlateNumber";
this.listBoxPLATES.ValueMember = "Index";

this.textBoxMAKE.DataBindings.Add("Text", vehicles,
"Make");
this.textBoxMODEL.DataBindings.Add("Text", vehicles,
"Model");
this.textBoxYEAR.DataBindings.Add("Text", vehicles,
"Year");

this.listBoxPLATES.DataBindings.Add("SelectedValue",
vehicles, "Index");
}

public Form1()
{
InitializeComponent();
this.InitializeArrays();
this.BindEmAll();
}

}
}
 
U

Uncle Sammy

Thanks Russell,

Actually I got it to "work" with the following code adjustments (see
code later on). I also added two buttons to move through the vehicles
array. A couple of interesting things:

1. By commenting out the Set properties in my vehicles and plates
classes, the index for vehicles doesn't change when I select an item in
the listbox.

2. With the code I have now, changing the BindingContext position in
the vehicles array changes the selected item in the listbox. Why then
doesn't the BindingContext position in the plates array do likewise to
the textboxes? By the former working, it implies that those indexes of
the two arrays are "bound together" now...?

Any help on this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BindingExample2
{
public partial class Form1 : Form
{
public class LicensePlate
{
private string plate;
private int index;
public string PlateNumber
{
get
{
return this.plate;
}
//set
//{
// this.plate = value;
//}
}
public int Index
{
get
{
return this.index;
}
//set
//{
// this.index = value;
//}
}

public LicensePlate(string _plate, int _index)
{
//this.PlateNumber = _plate;
//this.Index = _index;
this.plate = _plate;
this.index = _index;
}
}

public class Vehicle
{
private string make;
private string model;
private int year;
private int index;
public string Make
{
get
{
return this.make;
}
//set
//{
// this.make = value;
//}
}
public string Model
{
get
{
return this.model;
}
//set
//{
// this.model = value;
//}
}
public int Year
{
get
{
return this.year;
}
//set
//{
// this.year = value;
//}
}
public int Index
{
get
{
return this.index;
}
//set
//{
// this.index = value;
//}
}

public Vehicle(string _make, string _model, int _year, int
_index)
{
this.make = _make;
this.model = _model;
this.year = _year;
this.index = _index;
}
}

private LicensePlate[] plates;
private Vehicle[] vehicles;

private void InitializeArrays()
{
vehicles = new Vehicle[] {
new Vehicle("Ford", "Mustang", 1969, 1),
new Vehicle("Chevy", "Nova", 1965, 2),
new Vehicle("Dodge", "Van", 1975, 0)
};
plates = new LicensePlate[] {
new LicensePlate("YVX 809", 1),
new LicensePlate("BAD 001", 2),
new LicensePlate("3M TA3", 0)
};
}

private void BindEmAll()
{
this.listBoxPLATES.DataSource = plates;
this.listBoxPLATES.DisplayMember = "PlateNumber";
this.listBoxPLATES.ValueMember = "Index";

this.textBoxMAKE.DataBindings.Add("Text", vehicles,
"Make");
this.textBoxMODEL.DataBindings.Add("Text", vehicles,
"Model");
this.textBoxYEAR.DataBindings.Add("Text", vehicles,
"Year");

this.listBoxPLATES.DataBindings.Add("SelectedValue",
vehicles, "Index");
}

public Form1()
{
InitializeComponent();
this.InitializeArrays();
this.BindEmAll();
}

private void button1_Click(object sender, EventArgs e)
{
this.BindingContext[vehicles].Position -= 1;
}

private void button2_Click(object sender, EventArgs e)
{
this.BindingContext[vehicles].Position += 1;
}

private void listBoxPLATES_SelectedIndexChanged(object sender,
EventArgs e)
{
this.BindingContext[vehicles].Position =
this.listBoxPLATES.SelectedIndex;
}

}
}
 
R

Russell Mangel

Hoooooo Booooyyyy! You have been busy. If you keep this up I am going to
take away your keyboard license. However, for the time being I'll let you
keep your mouse license.

Seriously, it appears that you want this working and I can easily show you
how this works. But we will have to change a couple things.

1. If you want to continue using the textBox controls for display, then we
can't use the index value (delete) them, we can use the License Plate number
(string is fine), as the relationship. I am assuming the license plate
string will be be unique.

2. If you have to use the index to select a vehicle then the textboxes can
not be used, and you will need to use a complex binding capable control like
the dataGrid, or three combo boxes.

In your modified example you are not really doing Windows Forms Binding, you
are just forcing the list to move from the SelectedIndexChanged Event, so
you are not using binding correctly at all.

I will send you a sample 8 hours from now, I have to get some sleep. If you
get this message in time, please let me know what you would prefer Option 1
or Option 2.

Russell Mangel
Las Vegas, NV




Uncle Sammy said:
Thanks Russell,

Actually I got it to "work" with the following code adjustments (see
code later on). I also added two buttons to move through the vehicles
array. A couple of interesting things:

1. By commenting out the Set properties in my vehicles and plates
classes, the index for vehicles doesn't change when I select an item in
the listbox.

2. With the code I have now, changing the BindingContext position in
the vehicles array changes the selected item in the listbox. Why then
doesn't the BindingContext position in the plates array do likewise to
the textboxes? By the former working, it implies that those indexes of
the two arrays are "bound together" now...?

Any help on this?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BindingExample2
{
public partial class Form1 : Form
{
public class LicensePlate
{
private string plate;
private int index;
public string PlateNumber
{
get
{
return this.plate;
}
//set
//{
// this.plate = value;
//}
}
public int Index
{
get
{
return this.index;
}
//set
//{
// this.index = value;
//}
}

public LicensePlate(string _plate, int _index)
{
//this.PlateNumber = _plate;
//this.Index = _index;
this.plate = _plate;
this.index = _index;
}
}

public class Vehicle
{
private string make;
private string model;
private int year;
private int index;
public string Make
{
get
{
return this.make;
}
//set
//{
// this.make = value;
//}
}
public string Model
{
get
{
return this.model;
}
//set
//{
// this.model = value;
//}
}
public int Year
{
get
{
return this.year;
}
//set
//{
// this.year = value;
//}
}
public int Index
{
get
{
return this.index;
}
//set
//{
// this.index = value;
//}
}

public Vehicle(string _make, string _model, int _year, int
_index)
{
this.make = _make;
this.model = _model;
this.year = _year;
this.index = _index;
}
}

private LicensePlate[] plates;
private Vehicle[] vehicles;

private void InitializeArrays()
{
vehicles = new Vehicle[] {
new Vehicle("Ford", "Mustang", 1969, 1),
new Vehicle("Chevy", "Nova", 1965, 2),
new Vehicle("Dodge", "Van", 1975, 0)
};
plates = new LicensePlate[] {
new LicensePlate("YVX 809", 1),
new LicensePlate("BAD 001", 2),
new LicensePlate("3M TA3", 0)
};
}

private void BindEmAll()
{
this.listBoxPLATES.DataSource = plates;
this.listBoxPLATES.DisplayMember = "PlateNumber";
this.listBoxPLATES.ValueMember = "Index";

this.textBoxMAKE.DataBindings.Add("Text", vehicles,
"Make");
this.textBoxMODEL.DataBindings.Add("Text", vehicles,
"Model");
this.textBoxYEAR.DataBindings.Add("Text", vehicles,
"Year");

this.listBoxPLATES.DataBindings.Add("SelectedValue",
vehicles, "Index");
}

public Form1()
{
InitializeComponent();
this.InitializeArrays();
this.BindEmAll();
}

private void button1_Click(object sender, EventArgs e)
{
this.BindingContext[vehicles].Position -= 1;
}

private void button2_Click(object sender, EventArgs e)
{
this.BindingContext[vehicles].Position += 1;
}

private void listBoxPLATES_SelectedIndexChanged(object sender,
EventArgs e)
{
this.BindingContext[vehicles].Position =
this.listBoxPLATES.SelectedIndex;
}

}
}
 
R

Russell Mangel

What version of Visual Studio are you using?

Visual Studio 2002 (1.0)
Visual Stuido 2003 (1.1)
Visual Studio 2005 (2.0)
 

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