
Signature
Happy Coding!
Morten Wennevik [C# MVP]
Hi Morten,
Thank-you for the reply; I was going to add this code before, and then
saw your reply. It's code I downloaded from a URL referenced in MSDN,
and as you'll see, the code does implement IBindingList, but the
constructor is completely empty. It sets a private type field that's
later used in the IBindingList.AddNew() implemented function:
public class MyCollection : ArrayList, IBindingList, ITypedList
{
private Type finalType = typeof(MyData);
public MyCollection() { }
bool IBindingList.AllowNew
{
get { return true ; }
}
object IBindingList.AddNew()
{
return finalType.GetConstructor(new Type[]{}).Invoke(null);
}
// Additional implementation of IBindingList
}
public class FakeData
{
private FakeData(){}
public static MyCollection GetData()
{
MyCollection collection = new MyCollection();
for (int i = 0; i < 10; i++)
{
MyData ii = new MyData();
ii.ID = i;
ii.FirstName = "aaa" + i.ToString();
ii.LastName = "bbb" + i.ToString();
collection.Add(ii);
}
return collection;
}
}
public class MyData
{
private int id = 0;
private string firstName = null;
private string lastName = null;
public MyData() {}
public int ID
{
get{ return id; }
set{ id = value; }
}
public string FirstName
{
get{ return firstName; }
set{ firstName = value; }
}
public string LastName
{
get{ return lastName; }
set{ lastName = value; }
}
}
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataGridView dataGrid1;
private DataGridView dataGrid2;
BindingList<MyData> arrList2 = new BindingList<MyData>();
MyData myData1 = new MyData();
public Form1()
{
InitializeComponent();
myData1.ID = 1;
myData1.FirstName = "ccc";
myData1.LastName = "cccc";
arrList2.Add(myData1);
dataGrid1.DataSource = FakeData.GetData();
dataGrid2.DataSource = arrList2;
}
}
Even though MyCollection implements IBindingList, the
IBindingList.AddNew() never gets called because dataGrid1 doesn't show
the new empty row, while dataGrid2 does.
MyCollection appears to also implement ITypedList, although I haven't
made heads or tails of what's all involved there yet.
So is dataGrid1 basically just binding to an ArrayList here?
On Jan 7, 1:32 am, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.com> wrote:
> Hi TMall,
>
[quoted text clipped - 7 lines]
> Happy Coding!
> Morten Wennevik [C# MVP]
Marc Gravell - 07 Jan 2008 14:03 GMT
Your AddNew has *created* a new object, but it hasn't *added* it.
object obj = Activator.CreateInstance(finalType);
Add(obj);
return obj;
Any better?
Out of idle curiosity, why not use generics (with the new()
constraint)? Or the existing List<T> / BindingList<T>? Any huge issue
you are trying to avoid?
Marc
t.mall10@yahoo.com - 07 Jan 2008 14:56 GMT
> Your AddNew has *created* a new object, but it hasn't *added* it.
>
[quoted text clipped - 9 lines]
>
> Marc
BTW, you were absolutely right about adding the object to the list; it
solved the infinite error I was getting. Thanks again!
Marc Gravell - 07 Jan 2008 14:06 GMT
For info, the following has some examples of this (look at figure
5/6); OK it is in VB, but we'll forgive it...
http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/
Marc
t.mall10@yahoo.com - 07 Jan 2008 14:48 GMT
> For info, the following has some examples of this (look at figure
> 5/6); OK it is in VB, but we'll forgive it...http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBind...
>
> Marc
Thanks for the URL reference - turns out that the implemented
IBindingList property SupportsChangeNotification was set to "false" in
MyCollection; changing that to "true" allowed the grid to show the new
row, but now an infinite loop error is popping up, so it's time for
some more debugging, but at least the new row is visible now.
I remember seeing that that the AllowNew property had to be set to
true, but didn't make the connection that this other property also had
to be true.
In answer to your other post, I actually am using List<T> in the base
class of this data access layer, but I want to add databinding, and
that's where I wasn't sure to start over and derive from
BindingList<T>, or to keep going in the same vein of things and tack
on IBindingList, and other interfaces if/when necessary.
Thanks again for all the replies, TMall