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
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.
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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"
/>
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.
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
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
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 >>