tags:

views:

36

answers:

2

I have a BindingList for DTO which can bind directly to window form control and the problem was raised when user want to delete a row in my datagridview.

In that situation, the deleted object (row) go away and when that DTO BindingList get back to server for updating, I dont know which row to delete.

Can a BindingList have a collection for deleted object by default or I have to implement it manually or there is another option for my situation?

Any helps would be appreciated.

A: 

The BindingList has a ListChanged event that is raised when the list changes. You would need to write code that handles this event and tracks what items in the list were deleted.

http://msdn.microsoft.com/en-us/library/ms132742.aspx

Another option would be to track the deletes as they happen in the DataGridView.

Walter
Hi Walter, thanks a lot for your quick reply. The ListChanged event fired after the object is remove from the list, so I can't get a reference to a deleted item at this point.I can have a list within my BindingList which keep all the original item and get the deleted item from there, but i think that's not a good idea.Tracking the deletes from the DataGridView is not an option for me, cause I build a service to provide DTO object for my customer so I can not control what happen in the client side.Do you have any other idea for this ?
TheMy
@TheMy - Is your bindinglist a custom class that inherits BindingList (ie. MyObjectList:BindingList) or is it a BindingList Of MyObject?
Walter
Hi Walter, my BindList is a custom class that inherit from BindingList and it play as a BindingList for my custom object, below is the declaration for my class:public class DataList<T> : BindingList<T> where T : DataTransferBase
TheMy
Hi Walter, i try to override the RemoveItem function of BindingList to implement the tracking for deleted item and it seem to be ok, except that when user delete the last row in datagridview, the datagridview automatically add new row (blank row) and delete that row and ... do some thing else. After that my BindingList end up with 2 rows in the deleted item list, if I set AllowUserToAddRows to false, then it works perfectly. What is the default behaviour for DataGridView when user delete item ?
TheMy
A: 

In my opinion, get data manually with LinQ. Create a DataTable for result like

DataTable dt = new DataTable();

        dt.Columns.Add("Want to Delete?",typeof(bool));
        dt.Columns.Add("Data Id", typeof(string));
        dt.Columns.Add("Data 1", typeof(string));
        dt.AcceptChanges();

        return dt;

then create rows for your result like.

in a loop foreach(var result in myResult)

object[] row = new object[]
        {
        false,
        result.Id,
        result.Data1
        };
        dt.Rows.Add(row);

Let user can choice rows for delete, user can check first column. Because it is bool(checkBox) and after delete button click event, handle selected rows

foreach (DataRow dr in yourDataTable.Rows)
        {
            if (Convert.ToBoolean(dr[0])) // goes in if its checked
            {
                // delete dr[0]
            }
        }

I hope this will help you.

Serkan Hekimoglu
Hi Serkan, I need to apply changes back to the database within 1 transmission, your solution works fine with a simple object but I'm affraid that it cannot work with a more complex object (object with child relation ...), the child objects get update first and when user want to cancel edit for the main object, the deleted child object can not be recovered. Thanks.
TheMy