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

Add new row not working

5 Answers 140 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jessica
Top achievements
Rank 1
Jessica asked on 17 Jan 2012, 10:04 PM

When I set my grids datacontext as follows the Add new Row does not work:

dataGridView.DataContext = linqToSqldataContext.GetTable<my_Table_name>().AsEnumerable();

But If I set my datacontext like this it works but then my data in the grid is not binded to the source anymore:
dataGridView.DataContext = linqToSqldataContext.GetTable<my_Table_name>().AsEnumerable().ToList(); 

Can you explain why this is?


This is just a simplified version of what I am doing. Ideally I want to do the following and have all the data binded without having to process inserts/deletes/and updates for my data source.
dataGridView.DataContext  = (linqToSqldataContext.GetTableByName(TableNames.SelectedItem.ToString()));

where GetTableByName is 
static class DataContextExtensions
   {
       public static ITable GetTableByName(this DataContext context, string tableName)
       {
           if (context == null)
           {
               throw new ArgumentNullException("context");
           }
           if (tableName == null)
           {
               throw new ArgumentNullException("tableName");
           }
           var dataTable = context.GetType().GetProperty(tableName).GetValue(context, null);
           return (ITable)dataTable;
       }
   }

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 18 Jan 2012, 07:37 AM
Hi,

 Generally the reason why you cannot add any new item, would be because the RadGridView does not know how to create the new object of the Item's type. This is due to the fact that the Item does not have a default constructor.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jessica
Top achievements
Rank 1
answered on 18 Jan 2012, 02:33 PM
Here is my item definition created from linq to sql:
 It does have the default constructor that takes no parameters.

 Any other ideas? If I change the RadGridView to a WPF DataGrid I am able to add new items and delete.

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.tableName")]
    public partial class tableName: INotifyPropertyChanging, INotifyPropertyChanged
    {
         
        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
         
        private int _ID;
         
        private string _Value;
         
    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    partial void OnValueChanging(string value);
    partial void OnValueChanged();
    #endregion
         
        public tableName()
        {
            OnCreated();
        }
         
        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
        public int ID
        {
            get
            {
                return this._ID;
            }
            set
            {
                if ((this._ID != value))
                {
                    this.OnIDChanging(value);
                    this.SendPropertyChanging();
                    this._ID = value;
                    this.SendPropertyChanged("ID");
                    this.OnIDChanged();
                }
            }
        }
         
        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Value", DbType="NVarChar(50)")]
        public string _Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                if ((this._Value != value))
                {
                    this.OnMode_ValueChanging(value);
                    this.SendPropertyChanging();
                    this._Value = value;
                    this.SendPropertyChanged("_Value");
                    this.OnMode_ValueChanged();
                }
            }
        }
         
        public event PropertyChangingEventHandler PropertyChanging;
         
        public event PropertyChangedEventHandler PropertyChanged;
         
        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }
         
        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


This is my where I declare my grid. All flags are set to true.

<telerik:RadGridView x:Name="dataGridView"
                                 ShowGroupPanel="False"
                                 ItemsSource="{Binding}"
                                 AutoGenerateColumns="True"
                                 ShowInsertRow="True"
                                 RowDetailsVisibilityMode="Visible"
                                 CanUserDeleteRows="True"
                                 CanUserInsertRows="True"/>
0
Dimitrina
Telerik team
answered on 20 Jan 2012, 10:59 AM
Hello,

 I have tested binding to IEnumerable and indeed all the data is properly shown, but you cannot automatically insert new data rows. This is the expected behaviour when the ItemsSource is IEnumerable.

I have tested the same approach with the DataGrid, but it did not even populated the data.

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Jessica
Top achievements
Rank 1
answered on 20 Jan 2012, 06:45 PM
What I need to do is read, update, insert and delete items from an SQL server database quickly and efficiently.

I see that telerik has ORM http://www.telerik.com/products/orm.aspx.

If I use ORM instead of Linq to SQL will I be able to perform add delete update operations out of the box. Or will I have the same issue I am having right now with Linq To SQL.

Thanks,
-Jessica
0
Dimitrina
Telerik team
answered on 23 Jan 2012, 09:35 AM
Hi Jessica,

 It depends on the collection bound to the RadGridView. If it is IEnumerable, then the result will be the same.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Jessica
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Jessica
Top achievements
Rank 1
Share this question
or