I'm trying to implement a basic CRUD form using the GridView. I am using EF to populate the grid. I need to set some default values and found this link which shows using the DefaultValuesNeeded event. When I attempt to use this I get a NullReferenceException.
I'm obviously missing something here, but I'm not sure what.
Are there any examples that show:
Here's my currently working code for viewing the records:
And this is what the code in the DefaultValuesNeeded event looks like:
I'm obviously missing something here, but I'm not sure what.
Are there any examples that show:
- Binding to records retrieved from EF
- Setting default values when adding new records
- Where to hook in saving changes when a record is added/updated/deleted
- Displaying validation errors
Here's my currently working code for viewing the records:
var context =
new
FlockWatchEntities();
var forecast = context.ForecastMSTs.Where(i => i.Type ==
"F"
).OrderBy(i => i.chrDesc);
grdForecast.DataSource = forecast;
And this is what the code in the DefaultValuesNeeded event looks like:
private
void
grdForecast_DefaultValuesNeeded(
object
sender, Telerik.WinControls.UI.GridViewRowEventArgs e)
{
e.Row.Cells[
"dtmEntered"
].Value = DateTime.Now;
e.Row.Cells[
"chrEnteredBy"
].Value = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
5 Answers, 1 is accepted
0
Hi Paul,
I hope this helps. Regards,
Julian Benkov
the Telerik team
You can use the user events of RadGridView control to implement this functionality. Currently, we not have an example in the context of EF. Here is a sample example of RadGridView implementing autosave functionality using these events:
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.CellValidating +=
new
Telerik.WinControls.UI.CellValidatingEventHandler(radGridView1_CellValidating);
this
.radGridView1.RowsChanged +=
new
Telerik.WinControls.UI.GridViewCollectionChangedEventHandler(radGridView1_RowsChanged);
}
void
radGridView1_CellValidating(
object
sender, Telerik.WinControls.UI.CellValidatingEventArgs e)
{
}
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 >>
0
Bob
Top achievements
Rank 2
answered on 08 Jun 2012, 08:50 PM
This is exactly what I am looking for...except in EF.
Any chance somene can provide an example on performing these CRUD Operations using EF?
Thanks
Bob
Any chance somene can provide an example on performing these CRUD Operations using EF?
Thanks
Bob
0
Hello Bob,
Julian Benkov
the Telerik team
Please view the answer in your previous forum post.
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 >>
0
Peter
Top achievements
Rank 1
answered on 09 Nov 2012, 06:30 AM
The link in the last reply is broken: "Please view the answer in your previous forum post."
Can I also please have some Entity Framework CRUD examples using Telerik Controls? I don't want to use the auto-binding directly to the EDMX, I want to have a layered architecture.
Can I also please have some Entity Framework CRUD examples using Telerik Controls? I don't want to use the auto-binding directly to the EDMX, I want to have a layered architecture.
0
Hi Peter,
You can use the Local property of your DbContext EntityFramework object to support this functionality. Please review the attached example for details. Soon our documentation will be updated with an article regarding this subject.
Thank you for your time and cooperation.
Regards,
Julian Benkov
the Telerik team
You can use the Local property of your DbContext EntityFramework object to support this functionality. Please review the attached example for details. Soon our documentation will be updated with an article regarding this subject.
Thank you for your time and cooperation.
Regards,
Julian Benkov
the Telerik team