This article will demonstrate the Insert, Update and Delete Operations in a Telerik Grid View.

It is a continuation of my earlier article - Telerik Grid View in ASP.NET MVC2- Part 1 ( http://www.c-sharpcorner.com/uploadfile/2124ae/9566/ ) - in which I have shown how to display data in a Telerik Grid.

So if you are coming directly to this article - I would recommend you go through Telerik Grid View - Part 1. I will be using the project I created in Part 1

So you can download it and follow the below steps to include the Insert , Update and Delete functionality in Telerik Grid View.

1. Model - Update Repsitory Class

try
{

entities.MyArticles.AddObject(new MyArticle()
{
Title = tempVM.sTitle,
Section = tempVM.sSection,
Views = tempVM.iViews,
Downloads = tempVM.iDownloads,
Status = tempVM.sStatus

});
entities.SaveChanges();
return 1;
}

catch (Exception ex)
{
return 0;
}
}


2. Controller - Include methods for Insert, Update and Delete Action

//route values defining the grid state - current page, sort expression, filter etc.
RouteValueDictionary routeValues=this.GridRouteValues();
if (TryUpdateModel(objArticles))
{
int success_Insert = objRepository.MyArticles_InsertRecord(objArticles);
if (success_Insert == 1)
{
TempData["UploadValidationMessage_Success"] = "Insert Operation Succeded!!";
}
else
{
TempData["UploadValidationMessage_Failure"] = "Insert Operation Failded!!";
}
}

return RedirectToAction("Index",routeValues);

}

3. View - Update Telerik Grid View

.Columns(columns=>
{
columns.Bound(p=>p.sTitle).Title("Title").Width(100);
columns.Bound(p=>p.sSection).Title("Section").Width(200);
columns.Bound(p=>p.iViews).Title("Views").Width(100);
columns.Bound(p=>p.iDownloads).Title("Downloads").Width(100);
columns.Bound(p=>p.sStatus).Title("Status").Width(120);
columns.Command(commands =>
{
commands.Edit().ButtonType(GridButtonType.ImageAndText);
commands.Delete().ButtonType(GridButtonType.ImageAndText);
}).Width(180).Title("Commands");

}).Pageable()
.Sortable()
.Filterable()
%>


4. Update Index.aspx - to include the Success or Faliure message


<span style="color:Red;font-weight:bold">
<%=TempData["UploadValidationMessage_Failure"]%>
</span>
<span style="color:Green;font-weight:bold">
<%=TempData["UploadValidationMessage_Success"]%>
</span
>


5. Run the application

Telerik.gif

Here we have seen how to perform Insert , Update and Delete operations in Telerik Grid View.

Happy Learning!