- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Deleting from DataGrid
Joined: 18-May-2006
Using Vs2005, SQLServer2000, LLBLGenPro, SelfServicing, TwoClass
I have a 1:n scenario where the collections are AgeGrpSys(1) and AgeGrp (m).
I have a form that allows navigation through AgeGrpSys and contains a textbox for the name of each AgeGrpSys and a datagrid for editing the AgeGrps.
I have set this up as follows:-
AgeGrpSys has PK AgeGrpSysID. AgeGrp has a composite PK of AgeGrpSysID and AgeGrpCode. These are all Integers. The tables are related on AgeGrpSysID.
I have a form containing a text box for the name of the AgeGrpSys and a DataGrid for AgeGrp. This is a DevExpress grid.
I have instantiated the AgeGrpSys Collection in the form as AgeGrpSysColl.
I have a BindingNavigator named AgeSysBindNavig to cycle through AgeGrpSys.
I have two BindingSources:- AgeSysBindSrc has AgeGrpSysColl as its DataSource and is the DataSource for BindingNavigator. AgeGrpBindSrc has AgeSysBindSrc as its DataSource and AgeGrp as its DataMember. This is the source for the DataGrid.
Thus, I have not had to instantiate an AgeGrp collection.
My problem is that I cannot delete rows from the grid because the AllowRemove of the underlying collection is set to its default value of False.
Is there any way that I can set it to True?
If not then I'd be pleased if anyone could give me an indication of how I should design this set up.
I instantiated the various components through the designer. My hand coding is as follows:-
Public Class fmAgeGroupings
Dim uow As UnitOfWork
Private Sub fmAgeGroupings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim filter As IPredicateExpression = New PredicateExpression()
uow = New UnitOfWork
AddHandler Me.AgeGrpDataGrid.EmbeddedNavigator.ButtonClick, AddressOf gridNavigBtnClickHandler
Me.AgeGrpSysColl.GetMulti(filter)
Me.AgeSysBindSrc.DataSource = Me.AgeGrpSysColl
Me.AgeGrpBindSrc.DataSource = Me.AgeSysBindSrc
Me.AgeGrpBindSrc.DataMember = "AgeGrp"
Me.txtAgeGrpSysName.DataBindings.Add( _
New Binding("Text", Me.AgeSysBindSrc, "AgeGrpSys", True))
Me.AgeSysBindNavig.BindingSource = Me.AgeSysBindSrc
Me.AgeGrpDataGrid.DataSource = Me.AgeGrpBindSrc
End Sub
Private Sub fmAgeGroupings_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Unbind grid
If Not Me.AgeGrpDataGrid.DataSource Is Nothing Then
Me.GridView1.CloseEditor()
Me.AgeGrpDataGrid.DataSource = Nothing
Me.AgeSysBindNavig.BindingSource = Nothing
Me.txtAgeGrpSysName.DataBindings.RemoveAt(0)
Me.AgeSysBindSrc.DataMember = Nothing
Me.AgeGrpBindSrc.DataSource = Nothing
Me.AgeSysBindSrc.DataSource = Nothing
End If
Me.AgeGrpSysColl.SaveMulti(True)
uow.Commit(New Transaction(IsolationLevel.ReadCommitted, "UOW"), True)
End Sub
Private Sub AgeGrpSysColl_BeforeRemove(ByVal sender As Object, ByVal e As System.EventArgs) Handles AgeGrpSysColl.BeforeRemove
uow.AddForDelete(sender)
End Sub
Private Sub gridNavigBtnClickHandler(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs)
Dim currAgeGrpSysID As Integer
Dim currAgeGrpCode As Byte
If e.Button.ButtonType = DevExpress.XtraEditors.NavigatorButtonType.Remove Then
If MessageBox.Show("Do you really want to delete the record?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
e.Handled = True
Else
currAgeGrpSysID = GridView1.GetFocusedRowCellValue(GridView1.Columns.ColumnByFieldName("AgeGrpSysID"))
currAgeGrpCode = GridView1.GetFocusedRowCellValue(GridView1.Columns.ColumnByFieldName("AgeGrpCode"))
Dim AgeGrp As AgeGrpEntity = New AgeGrpEntity(currAgeGrpSysID, currAgeGrpCode)
uow.AddForDelete(AgeGrp)
End If
End If
End Sub
End Class
When your bindingnavigator changes index to a new AgeGrpSys, you should set the AgeGrpSys.AgeGrp's AllowRemove to true. This should then thus be done in an event handler to the event of the BindingNavigator control when it changes index, after it has changed. So it changes, new agegrpsys is selected, you set the AgeGrp's AllowRemove to true and continue.
You can also walk the collection of AgeGrpSys objects prior to binding it to the datasource and set for all the AgeGrpSys objects the AgeGrp's AllowRemove property to true.
Be aware that removing from the AgeGrp collection doesn't automatically remove the entity from the database. Removing from a collection requires you to do the delete manually by adding the delete action to a UnitOfWork object for example and then commit the delete actions in one go later on.
Joined: 18-May-2006
Yes, thanks Otis - I'm pleased to say I'd worked it out myself.
Here is the extra code in case anyone else is interested.
Private Sub AgeSysBindSrc_PositionChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles AgeSysBindSrc.PositionChanged
If Me.AgeSysBindSrc.Position > -1 Then
Me.AgeGrpSysColl(Me.AgeSysBindSrc.Position).AgeGrp.AllowRemove = True
End If
End Sub