DatagridView and binding

DatagridView and binding

Old forum URL: forums.lhotka.net/forums/t/3413.aspx


ravimaran posted on Wednesday, August 22, 2007

Hi,  I am running into this issue on binding data to the datagridview. Actually I have collection of objects binded to the binding source:

my Form1

Form_Load(...){
   //bsList - binding source
  //dgvList - DataGridView
  // I have already attached dgvList.DataSource = bsList;
  bsList.DataSource = collectionOfChildrend;
}

Event_AddNewChild(){
  Form frm = New Form2(passing reference to the collectionList);
  frm.ShowDialog(this);
}


my Form 2
_collectionList aList;

Form2(collectionList)
{
  _collectionList = collectionList
}
Event_Save(){
  //child object is created
 _collectionList.Add(child)
}

Some cases I see the dgvList rebinds right away when there were child objects exsisted on the collectionList and it doesnt' rebinds when collectionList was empty.  I would really like to know how I can control this binding without it binds atuomatically.  I would like to rebinds the datasouce to gridview after I added the child object to the collection.  How do I do this?  I hope someone can help me with this!  Thank you.

RockfordLhotka replied on Wednesday, August 22, 2007

You'd have a far easier time of this if you use Windows Forms 2.0 data binding- which is to say that you should use a BindingSource control.

I spent no time testing the use of 1.x style data binding in Windows Forms 2.0 for CSLA .NET, because it was fundamentally limited, and Microsoft fixed most of the issues through the BindingSource.

ravimaran replied on Wednesday, August 22, 2007

actually I am using widows form 2.0.  Also I am having this another issue when I delete child objects from the collection.
Like I said earlier in from1 I have a regular datagridview and I am able to select a row and click the delete button.  I can see that the objects are being removed from the collection.  When only one child is left in the collection and when I delete that child, I don't know what I am doing, but all the child objects marked for deletion are marked for not deletion.  That is the objects are not being deleted and I can see all the objects are in the collection and binds to the gridview. 

I am guessing something I am missing thats why I can't delete the single item in the list and the gridview datasource is not updated as well. 
I hope I made sense. Please explain me if I am doing anything wrong.  Thank you.

RockfordLhotka replied on Wednesday, August 22, 2007

Yes, I understand that you are using Windows Forms 2.0. But your problem description makes it sound like you are binding the grid directly to the object, rather than binding the grid to a bindingsource and the bindingsource to the object.

 

Also, make sure you are running at least 2.1.4, or better yet 3.0.1, because there are important collection-based bug fixes in BLB all the way up to 3.0.

 

Rocky

 

 

From: ravimaran [mailto:cslanet@lhotka.net]
Sent: Wednesday, August 22, 2007 6:12 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] DatagridView and binding

 

actually I am using widows form 2.0.  Also I am having this another issue when I delete child objects from the collection.
Like I said earlier in from1 I have a regular datagridview and I am able to select a row and click the delete button.  I can see that the objects are being removed from the collection.  When only one child is left in the collection and when I delete that child, I don't know what I am doing, but all the child objects marked for deletion are marked for not deletion.  That is the objects are not being deleted and I can see all the objects are in the collection and binds to the gridview. 

I am guessing something I am missing thats why I can't delete the single item in the list and the gridview datasource is not updated as well. 
I hope I made sense. Please explain me if I am doing anything wrong.  Thank you.


ravimaran replied on Wednesday, August 22, 2007

Thank you for the reply Rocky,  I am really happy that you are replying realy fast.  I am sorry I didn't say it explicitly that I am binding the gridview to binding source and binding source to the collection.  I am able to over come the issue binding the binding source to the objects again after a child is added to the collection.  So I guess I need get the latest version of framework for the deletion part.  I am just curious when I delete the child objects one by one the collection length is reduced but when I delete the last item the length is back to its original value so no items get deleted.  I hope I am not asking any inefficent question. I am really new to csla.net but I really love the frame work.  Hope you can clear it for me.  Thank you.

RockfordLhotka replied on Thursday, August 23, 2007

It is possible that the bug fixes in 3.0 will help you. There were some cases where ListChanged was raised too early and data binding was told of a delete before the delete operation was entirely completed. I don’t know if that’s your issue, but it may be.

 

Rocky

 

From: ravimaran [mailto:cslanet@lhotka.net]
Sent: Wednesday, August 22, 2007 10:09 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] RE: DatagridView and binding

 

Thank you for the reply Rocky,  I am really happy that you are replying realy fast.  I am sorry I didn't say it explicitly that I am binding the gridview to binding source and binding source to the collection.  I am able to over come the issue binding the binding source to the objects again after a child is added to the collection.  So I guess I need get the latest version of framework for the deletion part.  I am just curious when I delete the child objects one by one the collection length is reduced but when I delete the last item the length is back to its original value so no items get deleted.  I hope I am not asking any inefficent question. I am really new to csla.net but I really love the frame work.  Hope you can clear it for me.  Thank you.


ravimaran replied on Thursday, August 23, 2007

Hi Rocky, I did check the ListchangedEvent but no help.  This is exactly what is happening.
Case 1:
  When only one child exsists in the gridview and When I delete the child, I see the child is removed from the gridview.  After I pressed save and opens the gridview form again, I see the deleted child object.

Case 2:
 When there are, lets say, 5 child objects exsist in the gridview and when I delete 3 objects from it, I see the deleted objects are removed from the gridview and when I click save and reopen the gridview form I do not see the deleted objects which is good.  It actually deletes.

Case 3:
When I delete all the objects from gridview, I see all the objects are removed but when I reopen the gridview form I see all the objects are still there - no deletion.

I am really confused why this happening and I took several approaches in calling ApplyEdit(), CancelEdit(), BeginEdit(), and EndEdit(). 

here is my piece of code

// my list
_collectionList 
int index;
On_DeleteEvent(...)
{
   index = dataGridViewList.SelectedCells[0].RowIndex
  _collectionList.RemoveAt(index)
}

On_SaveEvent(...)
{
  _collectionList.Save();
}

I hope I have given you enough informtion so you can clearly see where I am standing.  Thank you for all the replies still.

Copyright (c) Marimer LLC