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

persisting Bindinglist of Business Objects back to the databse

2 Answers 87 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 21 Feb 2012, 08:56 AM
In this online doc topic: http://www.telerik.com/help/winforms/gridview-populating-with-data-binding-to-bindinglist.html

it shows how to bind a bindinglist of business objects to the gridview.  I can see that the ListChanged event can be trapped for insert and delete and changes persisted to the database.  my question is:  how does one persist edits made in the grid that immeditaly update the underlying binding list back to the database?  what bindinglist event should I subscribe to for the update?  how do I get the index of the edited row from this event?

Do you have an working example of persisting a gridview/bindinglist back to a database without using a LINQ context or an ORM?

Thanks,
Jonathan

2 Answers, 1 is accepted

Sort by
0
Jonathan
Top achievements
Rank 1
answered on 21 Feb 2012, 09:26 AM
Oh I see in the article below states:

"business object type that implements INotifyPropertyChanged and the list will automatically convert the PropertyChanged events to ListChanged events"

so that means I can ge the update too. cool! 

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


0
Julian Benkov
Telerik team
answered on 23 Feb 2012, 02:33 PM
Hello Jonathan,

You can read also our article about this topic here. The other alternative method for updating is to use a set of events of RadGridView control to achieve auto save functionality. Here is a sample:

using System;
using System.Data;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
 
namespace Lab
{
    public partial class AutoSavingDataForm : Form
    {
        private List<DataRowView> lastRemovedRows = new List<DataRowView>();
 
        public AutoSavingDataForm()
        {
            InitializeComponent();
 
            this.radGridView1.UserAddedRow += new Telerik.WinControls.UI.GridViewRowEventHandler(radGridView1_UserAddedRow);
            this.radGridView1.UserDeletingRow += new Telerik.WinControls.UI.GridViewRowCancelEventHandler(radGridView1_UserDeletingRow);
            this.radGridView1.UserDeletedRow += new Telerik.WinControls.UI.GridViewRowEventHandler(radGridView1_UserDeletedRow);
            this.radGridView1.CurrentRowChanged += new Telerik.WinControls.UI.CurrentRowChangedEventHandler(radGridView1_CurrentRowChanged);
            this.radGridView1.CellValueChanged += new Telerik.WinControls.UI.GridViewCellEventHandler(radGridView1_CellValueChanged);
 
            this.radGridView1.RowsChanged += new Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(radGridView1_RowsChanged);
        }
 
        private void Form18_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'adventureWorksDataSet.Contact' table. You can move, or remove it, as needed.
            this.contactTableAdapter.FillBy(this.adventureWorksDataSet.Contact, null);
        }
 
        void radGridView1_RowsChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
        {
            if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
            {
                //your logic here...
            }
        }
 
        void radGridView1_CellValueChanged(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            IEditableObject editbaleObject = e.Row.DataBoundItem as IEditableObject;
            if (editbaleObject != null)
            {
                editbaleObject.EndEdit();
            }
 
            //update data
            DataRowView dataRowView = e.Row.DataBoundItem as DataRowView;
            if (dataRowView != null)
            {
                this.contactTableAdapter.Update(dataRowView.Row);
            }
        }
 
        void radGridView1_UserDeletingRow(object sender, Telerik.WinControls.UI.GridViewRowCancelEventArgs e)
        {
            for (int i = 0; i < e.Rows.Length; i++)
            {
                DataRowView dataRowView = e.Rows[i].DataBoundItem as DataRowView;
                if (dataRowView != null)
                {
                    this.lastRemovedRows.Add(dataRowView);
                }
            }
        }
 
        void radGridView1_CurrentRowChanged(object sender, Telerik.WinControls.UI.CurrentRowChangedEventArgs e)
        {
            if (e.OldRow == null)
            {
                return;
            }
 
            DataRowView dataRowView = e.OldRow.DataBoundItem as DataRowView;
            if (dataRowView != null)
            {
                DataRow dataRow = dataRowView.Row;
                if ( dataRow.RowState == DataRowState.Modified)
                {
                    this.contactTableAdapter.Update(dataRow);
                }
            }
        }
 
        void radGridView1_UserDeletedRow(object sender, Telerik.WinControls.UI.GridViewRowEventArgs e)
        {
            DataRow[] rows = new DataRow[this.lastRemovedRows.Count];
 
            for (int i = 0; i < this.lastRemovedRows.Count; i++)
            {
                rows[i] = this.lastRemovedRows[i].Row;
            }
 
            this.contactTableAdapter.Update(rows);
            this.lastRemovedRows.Clear();
        }
 
        void radGridView1_UserAddedRow(object sender, Telerik.WinControls.UI.GridViewRowEventArgs e)
        {
            DataRow[] rows = new DataRow[e.Rows.Length];
            for (int i = 0; i < e.Rows.Length; i++)
            {
                DataRowView dataRowView = e.Rows[i].DataBoundItem as DataRowView;
                if (dataRowView != null)
                {
                    rows[i] = this.lastRemovedRows[i].Row;
                }
            }
 
            this.contactTableAdapter.Update(rows);
        }
    }
}

I hope this helps.

Regards,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Tags
GridView
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Jonathan
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or