How to do Listboxes with master detail and default selection

U

Uwe Lesta

Hello NG,

I am despairing, please help me.

With VS2005 i drag 2 Listboxes on my form and a bindingsource.
Listbox1 is bind to a bindingSource1.DataSource = data
Listbox2 is bind to a Listbox2.DataSource = subItemsBindingSource

all work as expected. When i select a item in listBox1, listbox2 display the details.

In the next step i implement
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
listBox2.SelectedValue = _data[listBox1.SelectedIndex].SelectedSubItem.Name;
}
to select a default value for listbox 2. Fine.

Trouble starts when i try to record the changes of listbox2 with
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox2.SelectedIndex > 0)
_data[listBox1.SelectedIndex].SubItemName = (string)listBox2.SelectedValue;
}

What is the right / advised way to implement such a behaviour ?
Please Help.


----------------------
public class data
{
private string _name;
private string _subItemName;
private subData _selectedSubItem;
private List<subData> _subItems;

public string Name { get { return _name; } set { _name = value; } }
public string SubItemName { get { return _subItemName; } set { _subItemName = value; } }
public List<subData> SubItems { get { return _subItems; } set { _subItems = value; } }
public subData SelectedSubItem { get { return _selectedSubItem; } set { _selectedSubItem = value; } }
}

public class subData
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public subData(string n) { _name = n; }
}


public partial class Form1 : Form
{
public data[] _data;
public Form1()
{
InitializeComponent();
_data = new data[] {new data(), new data(), new data() };
_data[0].Name = "first";
_data[0].SubItemName = "first_2";
_data[0].SubItems = new List<subData>(new subData[] { new subData("first_1"), new subData("first_2"), new
subData("first_3") });
_data[0].SelectedSubItem = _data[0].SubItems[1];
_data[1].Name = "second";
_data[1].SubItemName = "second_3";
_data[1].SubItems = new List<subData>(new subData[] { new subData("second_1"), new subData("second_2"), new
subData("second_3") });
_data[1].SelectedSubItem = _data[1].SubItems[2];
_data[2].Name = "thhird";
_data[2].SubItemName = "thhird_2";
_data[2].SubItems = new List<subData>(new subData[] { new subData("thhird_1"), new subData("thhird_2"), new
subData("thhird_3") });
_data[2].SelectedSubItem = _data[2].SubItems[1];

bindingSource1.DataSource = _data;
}
}
 
G

Guest

What is the trouble that starts as I cant see off the top of y head what
would go wrong?


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Uwe Lesta said:
Hello NG,

I am despairing, please help me.

With VS2005 i drag 2 Listboxes on my form and a bindingsource.
Listbox1 is bind to a bindingSource1.DataSource = data
Listbox2 is bind to a Listbox2.DataSource = subItemsBindingSource

all work as expected. When i select a item in listBox1, listbox2 display the details.

In the next step i implement
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
listBox2.SelectedValue = _data[listBox1.SelectedIndex].SelectedSubItem.Name;
}
to select a default value for listbox 2. Fine.

Trouble starts when i try to record the changes of listbox2 with
private void listBox2_SelectedValueChanged(object sender, EventArgs e)
{
if (listBox2.SelectedIndex > 0)
_data[listBox1.SelectedIndex].SubItemName = (string)listBox2.SelectedValue;
}

What is the right / advised way to implement such a behaviour ?
Please Help.


----------------------
public class data
{
private string _name;
private string _subItemName;
private subData _selectedSubItem;
private List<subData> _subItems;

public string Name { get { return _name; } set { _name = value; } }
public string SubItemName { get { return _subItemName; } set { _subItemName = value; } }
public List<subData> SubItems { get { return _subItems; } set { _subItems = value; } }
public subData SelectedSubItem { get { return _selectedSubItem; } set { _selectedSubItem = value; } }
}

public class subData
{
private string _name;
public string Name { get { return _name; } set { _name = value; } }
public subData(string n) { _name = n; }
}


public partial class Form1 : Form
{
public data[] _data;
public Form1()
{
InitializeComponent();
_data = new data[] {new data(), new data(), new data() };
_data[0].Name = "first";
_data[0].SubItemName = "first_2";
_data[0].SubItems = new List<subData>(new subData[] { new subData("first_1"), new subData("first_2"), new
subData("first_3") });
_data[0].SelectedSubItem = _data[0].SubItems[1];
_data[1].Name = "second";
_data[1].SubItemName = "second_3";
_data[1].SubItems = new List<subData>(new subData[] { new subData("second_1"), new subData("second_2"), new
subData("second_3") });
_data[1].SelectedSubItem = _data[1].SubItems[2];
_data[2].Name = "thhird";
_data[2].SubItemName = "thhird_2";
_data[2].SubItems = new List<subData>(new subData[] { new subData("thhird_1"), new subData("thhird_2"), new
subData("thhird_3") });
_data[2].SelectedSubItem = _data[2].SubItems[1];

bindingSource1.DataSource = _data;
}
}
----------------------

Kind regards

Uwe
 
U

Uwe Lesta

Ciaran said:
What is the trouble that starts as I cant see off the top of y head what
would go wrong?

The trouble is:
- select a value in listbox2 and sore it in _data,
- select an other value in listbox1 and the wrong defailt value for Listbox2 is selected.

or from the internal view:
Every time listBox1_SelectedIndexChanged *also* listBox2_SelectedIndexChanged ( databinding )
with index = 0. so if i use this event to store a new selection i override my preselected value.

How to build this simple application ?
- Two listboxs with master detail databinding
- a default value ( _data.SelectedSubItem ) should be selected in listbox2 for each item in listbox1
- a new selection in listbox2 should be the new listbox2 default value.

listbox1 | listbox2
------------------------------------------------------
first first_1
first_2 ( default )
first_3
second second_1
second_2
second_3 ( default)

change the default value for listbox2 by select an other value
change the value in listbox1 and back again and the just selected
value in listbox2 should be selected.

or is it still impossible with master-detail databinding ?
 

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