Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > BindingSource.current resets property in object collection
 

BindingSource.current resets property in object collection

Hi

I've been searching the forums for a couple days to find the solution to this problem but I have had no luck.

I have a binding source on a windows form which is bound to a collection of type BindingList that holds a set of Portfolio objects. Each Portfolio object contains a collection which holds securities objects. The intention is that when the user selects a portfolio on the form, summary information for the securities are displayed. However I have noticed that when a portfolio object is set as the current object (bindingsource.current) it loses its securities. To make it a little clearer, when the application loads there are three portfolios, the first one is automatically displayed and should show the security information but it does not. While debugging I can see that the count for the securities property for this first portfolio is now zero (after being successfully loaded from the database), the count for the other two portfolios are correct. If I select portfolio2the securities are displayed, and if I then select portfolio 3, the securities are also displayed, but if I go back to 2 there are no securities and the count is now zero, the same occurs for 3. So it seems that after the object has been set as current, it loses the objects in the securities collection.

Could someone please help me resolve this? Thanks.

dneruck76  Wednesday, September 13, 2006 4:17 PM

Hi I found a solution to this.

I was doing some research and decided that I would need to implement a TypeDescriptionProvider class and a CustomTypeDescriptor. However after looking around a little more I found an implementation of a TypedBindingList that implements BindingList<T> and ITypedList at the link below.

http://www.winterdom.com/weblog/2006/07/27/ITypedListAndPropertyDescriptors.aspx

dneruck76  Friday, September 15, 2006 3:16 PM

Here is some code that might make it clearer. While debugging, I check the values in portfolioList and they are all correct. The count for the inner Securities collection (was originally a list but I changed it to BindingList to see if it would work) is correct for all three portfolios. The minute I set the datasource on portfolioBindingSource, the count for the securities collection in the first portfoliois zero. The value for the others are unchanged. The application therefore displays showing the first portfolio with no securities, but if I select portfolio 2 or 3, I can see the securities (because the securities collection still have values, however if I select them again, the securities no longer show and the count is set to zero.

public void BindToPortfolio(BindingList<Portfolio> portfolioList)

{

portfolioBindingSource.Clear();

portfolioBindingSource.DataSource = portfolioList;

}

dneruck76  Wednesday, September 13, 2006 9:55 PM

Try getting rid of the portfolioBindingSource.Clear()



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BindingTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Class1> lstCls = new List<Class1>();
BindingSource bs = new BindingSource();
BindingSource bsDetails = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
for (int x = 0; x < 20; x++)
{
Class1 cls = new Class1();
cls.ID = x;
cls.Name = "Item " + x.ToString();
Class2 l1 = new Class2();
Class2 l2 = new Class2();
Class2 l3 = new Class2();
l1.Name = "Apple " + x.ToString();
l2.Name = "Dog " + x.ToString();
l3.Name = "Cat " + x.ToString();
cls.MyList.Add(l1);
cls.MyList.Add(l2);
cls.MyList.Add(l3);
lstCls.Add(cls);
}
bs.DataSource = lstCls;
bsDetails.DataSource = lstCls[0].MyList;
dataGridView1.DataSource = bs;
dataGridView2.DataSource = bsDetails;
bs.PositionChanged += new EventHandler(PositionChanged);
}
private void PositionChanged(Object sender, EventArgs e)
{
bsDetails.DataSource=lstCls[bs.Position].MyList;
}

}
}

Class1



using System;
using System.Collections.Generic;
using System.Text;
namespace BindingTest
{
public class Class1
{
int _id = 0;
string _name = "";
List< Class2 > lst= new List<Class2 >();
public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public List<Class2> MyList
{
get
{
return lst;
}
set
{
lst = value;
}
}
}
}


Class 2



using System;
using System.Collections.Generic;
using System.Text;
namespace BindingTest
{
public class Class2
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
}


Ken Tucker  Thursday, September 14, 2006 1:31 AM

Hi Ken

Thanks for the reply. Removing the portfolioBindingSource.Clear doesn't make a difference.

I ran your code, which works fine. However in comparing to my scenario, where you set:

bs.DataSource = lstCls;
bsDetails.DataSource = lstCls[0].MyList;

and I set

portfolioBindingSource.DataSource = portfolioCol;

securitiesBindingSource.DataSource = portfolioCol[0].Securities;

The moment I set portfolioBindingSource.DataSource to portfolioCol, portfolioCol[0].Securities no longer contains values.


BTW portfolioBindingSource is binding to a combobox, and I am using the Smart Client Software factory, so what I am doing is passing the securities collection for the currently displayed portfolio to another view, which works fine. Its just for some reason as soon as the portfolio object is set as the current portfolio in the bindingsource it loses the values in the securities collection.

dneruck76  Thursday, September 14, 2006 2:22 PM

Hi I found a solution to this.

I was doing some research and decided that I would need to implement a TypeDescriptionProvider class and a CustomTypeDescriptor. However after looking around a little more I found an implementation of a TypedBindingList that implements BindingList<T> and ITypedList at the link below.

http://www.winterdom.com/weblog/2006/07/27/ITypedListAndPropertyDescriptors.aspx

dneruck76  Friday, September 15, 2006 3:16 PM

You can use google to search for other answers

Custom Search

More Threads

• DataBinding to a custom user control
• How to set the icon of a specific node in a treeview control?
• Updating database and calling stored procedure after CellLeave event
• data binding to multiple windows forms
• Hidden column in Datagridview collection shows back up when placed on tabcontrol page > 1
• DataGridView RowCount Bug
• how to update database using bindingsource
• Different data types in one Datagrid column
• Crystal Reports
• Paste into DataGridView