This is a migrated thread and some comments may be shown as answers.

Best place to update DB

28 Answers 619 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 2
Iron
Iron
Pierre asked on 29 Jan 2008, 02:11 PM
What is the best place to update the DB when a new row are added, deleted or modified in the Grid?

Validating?
Validated?
ValueChaging?
ValueChanged?

Thans a lot

28 Answers, 1 is accepted

Sort by
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 29 Jan 2008, 04:48 PM
I am quite lost. Meaby I not fast enough, but the documentation and the live example are not clear for me.

I use LINQ to fill my grid, now I try to found a way to update with LINQ the modified, added or deleted row. Where I can place my code, how to see if the row are new or modified, rowdeleted are in the avalaibel event but not rowEdited and rowAdded

The sample do not show the Grod to DB process, the validating or validated function are not documented (I do not found it) in the online help.

any suggestion?
Thanks
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 30 Jan 2008, 09:34 PM
Someone could give my the big line? I use the web radGrid in the prometheus suite, but is the first time that I need to use the winform grid, and I quite lost between the 2 grid to how manage data binding in winform grid.

thanks
0
Georgi
Telerik team
answered on 31 Jan 2008, 02:19 PM
Hello Pierre,

Perhaps the Validated event is that what you are looking for. It is the last event, which is fired when the editing of a cell is finished and the value has been saved in the data source. Unlike Validated, the Validating event is fired just before the editor is closed and can be canceled.
The ValueChanging/ValueChanged events are fired on every change the user makes in the editor. You could also see these posts for additional details:

    - http://www.telerik.com/community/forums/thread/b311D-bbeaaa.aspx

    - http://www.telerik.com/community/forums/thread/b311D-bahgmh.aspx

If you have any additional questions, do not hesitate to write us again.

Kind regards,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 07 Feb 2008, 06:33 PM
ok,  but how do I detect if the row is new or just updated? 

I try to make a 2-way databinding. But I can't found any example in your sample. All the sample show how to populate the grid from any source, but not how to update back the database in case of update, insert or delete row.
0
Georgi
Telerik team
answered on 08 Feb 2008, 06:44 PM
Hi Pierre,

After the Validated event is fired all changes should be saved in the memory in the data source object to which the grid is bound to. Further persisting in the underlying database depends on your choice. For example, if you are using ADO table and you have created a table adapter for this table, you could call its Update() method. Unfortunately, in the current version there is no standard way of distinguishing whether a new row has been added or an existing row has been changed.

As to deleting a row, you can use the RowDeleting and RowDeleted events.

Please let us know if we could be of any further assistance.
 

Kind regards,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 14 Feb 2008, 03:01 PM
your right, for now I use datatable but when we add new row in the grid, the validated event are called but the added row are not in the datasource. I presume that the row is added in the datasource juste after this event? How can I rise event when a new row is added to the backen memory datasource:

ctype(RadGridSegments.DataSource,DataTable).GetChanges(DataRowState.added)

For the modified row, its work correctly in the Validated event, but not the added row.

thanks

0
Georgi
Telerik team
answered on 15 Feb 2008, 02:01 PM
Hi Pierre,

As I have already mentioned, currently there is no way to update the database in the Validated event when a new row is added. We are working on a major refactoring of our RadGridView and soon the new version will be available with many improvements in the API.

Meanwhile, if your are using BindingSource to bind the RadGridView, you could attach to its ListChanged event and in the event handler you could make the update.

Please, try this approach and let us know whether it works fine for you.

Sincerely yours,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 19 Feb 2008, 05:01 PM
ok, I do not use a bindingSource control. I bind directly to the grid from a LINQ.IQueryable.

If I bind my LINQ.IQueryable to a binding source and thent bind this binding source to the grid. I am affraid that I will create a lot of thing in memory that can slow down the process?

Meaby I will wait for your grid update and for now put a simple Save button.
Do you have a time frame for this update?
0
Georgi
Telerik team
answered on 21 Feb 2008, 05:10 PM
Hello Pierre,

Thank you for writing.

Unfortunately, no other workaround is available at this moment. This functionality will be available in the new version of the RadGridView. The new RadGridView will be introduced with the next major release - Q1 2008 (expected in April). We expect to release a beta version of the Q1 release in the upcoming days, which will include a beta version of the new RadGridView.

If you have any additional questions, please write me.

All the best,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 22 Feb 2008, 08:05 PM
thanks for you anwser. It is possible to have a copy of this beta RadGrid when it become avalaible?

Thanks
0
Vassil Petev
Telerik team
answered on 25 Feb 2008, 08:27 AM
Sure Pierre,

We will update this thread as soon as the beta is available.

The beta will also be announced in the Announcements Forum which supports RSS feeds. I will suggest subscribing to the thread in order to get updated on all new announcements.


Sincerely yours,
Vassil
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 25 Feb 2008, 02:30 PM
Ok

Do I have a way in the current version of the rad grid to test if I can add a new row. For example I need to test if the name entered are alereadu used in the DB. If so, cancelling the added row (like the cancel = true of the deleted event for example).

I think I just have for now the ListChange of the BindingSource avalaible? I hope the grid beta will be avalable this week! ;-)

Thanks
0
Vassil Petev
Telerik team
answered on 26 Feb 2008, 11:31 AM
Hi Pierre,

Thank you for getting back to us.

The only way of doing this at the moment is to check if that value is contained in your data source. Consider the following code:

DataRow[] rows = table.Select("ID=10"); 
if (rows.Length > 0) 
     //...                 

Let us know, if you have other questions.
 

Kind regards,
Vassil
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Vassil Petev
Telerik team
answered on 04 Mar 2008, 03:19 PM
Hello Pierre,

We released the beta of RadGridView last week. You can find the download and details in the WinForms Beta Forum: http://www.telerik.com/community/forums/thread/b311D-bceebb.aspx


Greetings,
Vassil
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 04 Mar 2008, 07:33 PM
Thanks, I try this beta release. Do you have doc on how correctly using the new API command wiht myu scenario case?
0
Accepted
Georgi
Telerik team
answered on 06 Mar 2008, 03:42 PM
Hi Pierre,

We are still working on the documentation related to the new version of RadGridView, and we will release any documentation as soon as it becomes available. In the beta release, the documentation for RadGridView is not updated and it relates to the previous version.

Since we are continuing our work on the of the RadGridView , we will do our best to introduce the new help docs in the next major version - Q1 2008.

If you have any additional questions, I'll be happy to answer them.

Best wishes,
Georgi
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
David Sandor
Top achievements
Rank 1
answered on 14 Nov 2008, 03:45 PM
So with the new version of the GridView what is the answer to his original question.  I have similar issues.  I would ultimately like to know when the row is out of edit mode.  Otherwise I have to update my database for each cell that is edited.

Ultimately it would be great if we had a:  RowEditingEnd event so that we know that an add or edit is complete.  We could then mark the item dirty or update the database.

I am using LINQ and WCF so we do not bind to DataTables anymore.  I am using strongly typed objects.

Thanks,
David
0
Jack
Telerik team
answered on 18 Nov 2008, 03:18 PM
Hi David,

Thank you for this question.

Currently there is no such event in RadGridView. We plan to implement a new validation mechanism in our next release. Your suggestions will be taken in consideration when implementing this mechanism.

You could use the CurrentRowChanged event as a work around. This event is fired when the currently selected row is changed. Consider the following code snippet:

bool rowModified = false
 
void radGridView1_CellEndEdit(object sender, GridViewCellEventArgs e) 
    if (this.radGridView1.ActiveEditor.IsModified) 
    { 
        rowModified = true
    } 
 
void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e) 
    if (rowModified) 
    { 
        // update DB 
    } 
    rowModified = false
 


In case you need further assistance, do not hesitate to contact me.

Kind regards,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 30 Jun 2009, 02:01 PM
Hi, it me again!

Now in midle of 2009, do you have a real solution for that? I try your code snippet provided but is not a big deal. For example if the grid are in a popup windows, if the user close the windows before changing the selecetd row, the update do not occurd. We really need a function like rowEditingEnd that provide the type of modification (new or edited).

Do you have more info ?
0
Jack
Telerik team
answered on 01 Jul 2009, 09:21 AM
Hello Pierre,

In our upcoming Q2 release, we will introduce cell and row validation events. The editing process finishes when changing the row, so should simply handle the RowValidated event. There is no such event as RowEditingEnd and you should manually handle the case when RadGridView loses the focus. This can be done by handling OnLostFocus event.

Please send us a sample application that demonstrates your desired behavior and will try to find the best solution. I am looking forward to your reply.

Greetings,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 01 Jul 2009, 12:48 PM
the need is simple. I aleready bind manually data with Linq and I need to update de BD back manually. The virtual mode are too manual for my. I only need to found a way to update the DB back when the editor ended (support newow, editedt row and deleted row).

For now I test with this code:

    Private Sub RadGridSuper_CellEndEdit(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.GridViewCellEventArgs) Handles RadGridSuper.CellEndEdit  
        If RadGridSuper.ActiveEditor.IsModified Then  
            Dim dbPunchInfo As New PunchInfoDataContext  
 
            If e.RowIndex > -1 Then  
                'Row modified  
                Dim super As Superviseur = dbPunchInfo.Superviseurs.Single(Function(p As Superviseur) p.ID = Convert.ToInt32(RadGridSuper.Rows(e.RowIndex).Cells("ID").Value))  
                super.Nom = RadGridSuper.Rows(e.RowIndex).Cells("Nom").Value  
                dbPunchInfo.SubmitChanges()  
            Else  
                'new row  
                Dim super As New Superviseur  
                super.Nom = RadGridSuper.Rows(e.RowIndex).Cells("Nom").Value  
                dbPunchInfo.Superviseurs.InsertOnSubmit(super)  
                dbPunchInfo.SubmitChanges()  
            End If  
        End If  
    End Sub 

This case do not work well, because he do not handle the Deleted row. When a new row is added I can't acces the value added. It work only for edited cell.
 In this case I only have one column displayed on this grid. But in the near future I will have more column (cell) and the CellEndEdit will be called many time for the same row? I working witht the Q2 Beta, but the new RowValidated do not seam to be included.

I use Web Grid very often and this grid have a NeedDatasource, Insert, Update and Delete Event to controle the update to DB. I need something similar with the form grid.
0
Jack
Telerik team
answered on 03 Jul 2009, 04:36 PM
Hi Pierre,

No, the new validation events are not included in the beta. They will be available in the final release next week. Yes CellEndEdit will be called for each column, however you can monitor the ListChanged event of the associated CurrencyManager instead. This event is the equivalent  of the mentioned events in WinForms and it is called on every change in the database. Please consider the following code sample:

Dim manager As CurrencyManager = TryCast(Me.radGridView1.BindingContext(Me.radGridView1.DataSource, Me.radGridView1.DataMember),  
System.Windows.Forms.CurrencyManager) 
 
Private Sub manager_ListChanged(ByVal sender As ObjectByVal e As ListChangedEventArgs) 
    If e.ListChangedType = ListChangedType.ItemChanged Then 
        '... 
    End If 
End Sub 

I hope this helps. If you have more questions, please write me back.

Greetings,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 06 Jul 2009, 03:34 PM
Thanks for your reply. If you said that the new validation function avalaible this week will solve my problem (modified, deleted, added) I will wait for the relase. 
0
Jack
Telerik team
answered on 07 Jul 2009, 08:23 AM
Hi Pierre,

CellValidating and CellValidated events can help when monitoring cell value changes. You have to use the ListChanged event to monitor for added and removed rows.

Please write back if you need further assistance on that.

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Pierre
Top achievements
Rank 2
Iron
Iron
answered on 07 Jul 2009, 12:26 PM
Ok I try the currency code. But I have some problem to found the correct index and read the data currently in the grid. This code work well for editing, but the index are not valid for deleting and adding a new row.
    Private Sub m_CurrencyManager_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles m_CurrencyManager.ListChanged  
        Dim dbPunchInfo As New PunchInfoDataContext  
 
        Select Case e.ListChangedType  
 
            Case System.ComponentModel.ListChangedType.ItemAdded  
                m_Add = True 
 
            Case System.ComponentModel.ListChangedType.ItemDeleted  
                'Delete current row  
                Dim super As Superviseur = dbPunchInfo.Superviseurs.Single(Function(p As Superviseur) p.ID = Convert.ToInt32(RadGridSuper.Rows(e.NewIndex).Cells("ID").Value))  
                dbPunchInfo.Superviseurs.DeleteOnSubmit(super)  
                dbPunchInfo.SubmitChanges()  
 
            Case System.ComponentModel.ListChangedType.ItemChanged  
                If m_Add Then  
                    'Add new row  
                    Dim super As New Superviseur  
                    super.Nom = RadGridSuper.Rows(e.NewIndex).Cells("Nom").Value  
                    dbPunchInfo.Superviseurs.InsertOnSubmit(super)  
                    dbPunchInfo.SubmitChanges()  
                    m_Add = False 
                Else  
                    'Change current row  
                    Dim super As Superviseur = dbPunchInfo.Superviseurs.Single(Function(p As Superviseur) p.ID = Convert.ToInt32(RadGridSuper.Rows(e.NewIndex).Cells("ID").Value))  
                    super.Nom = RadGridSuper.Rows(e.NewIndex).Cells("Nom").Value  
                    dbPunchInfo.SubmitChanges()  
                End If  
        End Select  
    End Sub 
0
Jack
Telerik team
answered on 07 Jul 2009, 03:08 PM
Hi Pierre,

I understand. This row id is not valid when removing rows, because the event fires after removing the row. It will be better to use CollectionChanged event of the RadGridView.Rows collection, If you want to use data from the removed row. Here is a sample:

Private Sub Rows_CollectionChanging(ByVal sender As ObjectByVal e As NotifyCollectionChangingEventArgs) 
    If e.Action = NotifyCollectionChangedAction.Remove Then 
        Dim value As Integer = CInt(DirectCast(e.OldItems(0), GridViewDataRowInfo).Cells("ID").Value) 
            '... 
    End If 
End Sub 
 

When you click on "click here to add new row" a new row is added to the Rows collection. However, the process finishes when you exit from this row by clicking Enter for example. Because of this, you should handle the CurrentRowChanged event in this case. Please consider this sample:

Private addedRow As GridViewDataRowInfo 
 
Private Sub Rows_CollectionChanging(ByVal sender As ObjectByVal e As NotifyCollectionChangingEventArgs) 
    If e.Action = NotifyCollectionChangedAction.Add Then 
        addedRow = TryCast(e.NewItems(0), GridViewDataRowInfo) 
    End If 
End Sub 
 
Private Sub radGridView1_CurrentRowChanged(ByVal sender As ObjectByVal e As CurrentRowChangedEventArgs) 
    If addedRow IsNot Nothing Then 
        If Me.radGridView1.Rows.IndexOf(addedRow) >= 0 Then 
                '... 
            Dim id As Integer = CInt(addedRow.Cells("ID").Value) 
        End If 
        addedRow = Nothing 
    End If 
End Sub 
 

We realize that this is not the most convenient way to do this, so we will improve our API in one of our upcoming releases.

Should you have any questions, please write us.

Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Igor
Top achievements
Rank 1
answered on 20 Aug 2009, 02:14 PM
Hi, I solved this problem so:

    public class MyGridView : RadGridView 
    { 
        public MyGridView() : base() 
        { 
            isModified = false
            isRowEndEditing = false
             
            ThemeClassName = typeof(RadGridView).FullName; 
 
            CellEndEdit += MyRadGrid_CellEndEdit; 
        } 
 
        private bool isModified; 
        private bool isRowEndEditing; 
        private object RowEndEditEventKey = new object(); 
 
        public event GridViewRowEventHandler RowEndEdit 
        { 
            add 
            { 
                base.Events.AddHandler(RowEndEditEventKey, value); 
            } 
            remove 
            { 
                base.Events.RemoveHandler(RowEndEditEventKey, value); 
            } 
        } 
 
        public override bool EndEdit() 
        { 
            if (isModified) 
            { 
                isRowEndEditing = true
                isModified = false
            } 
 
            return base.EndEdit(); 
        } 
 
        private void RaiseRowEndEdit() 
        { 
            var args = new GridViewRowEventArgs(CurrentRow); 
            OnRowEndEdit(this, args); 
        } 
 
        private void OnRowEndEdit(object sender, GridViewRowEventArgs e) 
        { 
            var handler = (GridViewRowEventHandler) base.Events[RowEndEditEventKey]; 
 
            if (handler != null
            { 
                handler(sender, e); 
            } 
        } 
 
        private void MyRadGrid_CellEndEdit(object sender, GridViewCellEventArgs e) 
        { 
            if (ActiveEditor.IsModified) 
            { 
                isModified = true
            } 
 
            if (isRowEndEditing) 
            { 
                RaiseRowEndEdit(); 
                isRowEndEditing = false
            } 
        } 
    } 
 

I use MyGridView and I can subscribe for the event RowEndEdit and update or insert a entity in the database. For delete I use ListChanged event of a BindingSource.

Best regards
0
Nick
Telerik team
answered on 20 Aug 2009, 02:18 PM
Hi Igor Petrov,

Thank you for sharing your solution with the community.

Kind regards,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Pierre
Top achievements
Rank 2
Iron
Iron
Answers by
Pierre
Top achievements
Rank 2
Iron
Iron
Georgi
Telerik team
Vassil Petev
Telerik team
David Sandor
Top achievements
Rank 1
Jack
Telerik team
Igor
Top achievements
Rank 1
Nick
Telerik team
Share this question
or