TelerikDeveloper.com

TelerikDeveloper.com

sgray128 1/12/2015 3:22:32 PM

ASP.NET - RadGrid Editing: Insert, Update and Delete

This is a code sample for the RadGrid showing Inserts, Updates, and Delete. This technique uses a separate form for the editing


ASP.NET:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
                 CssClass="grid" AllowPaging="True" PageSize="300" Width="300px">
    <MasterTableView DataKeyNames="vchrTruckNumber" CommandItemDisplay="Top"  >
        <Columns>
            <telerik:GridButtonColumn ConfirmText="Delete this truck?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" ConfirmDialogHeight="100px"
                ConfirmDialogWidth="220px"  />
            <telerik:GridEditCommandColumn   ButtonType="ImageButton"></telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn DataField="vchrTruckNumber" HeaderText="Truck"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
 

.NET:

Imports FPCommon
Imports DynData
Imports Telerik.Web.UI
 
Partial Class utilities_TruckAndDriver
    Inherits System.Web.UI.Page
 
    Protected Sub utilities_Truck_Load(sender As Object, e As System.EventArgs) Handles Me.Load
 
    End Sub
 
    Protected Sub RadGrid1_DeleteCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.DeleteCommand
        'get the data key
        Dim strTruck As String = TryCast(e.Item, GridDataItem).GetDataKeyValue("vchrTruckNumber")
        'data access code to delete the item
        SPs.FP_Trucks_DEL_byID(strTruck, App.Database).execute()
    End Sub
 
    Protected Sub RadGrid1_EditCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.EditCommand
        'get the data key
        Dim strTruck As String
        strTruck = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("vchrTruckNumber").ToString()
        'redirect to another form to do the editing
        Response.Redirect("truckAddEdit.aspx?Truck=" & strTruck)
    End Sub
 
    Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
        'we only care about the Add New, Edit and Delete are taken care of in their own routines
        Select Case e.CommandName
            Case "Insert"
                'redirect to another form to do the insert
                Response.Redirect("truckAddEdit.aspx?Truck=0")
        End Select
    End Sub
 
    Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        'populate the grid
        Me.RadGrid1.DataSource = SPs.FP_TRUCKS_SEL2(App.Database).getTable
    End Sub
End Class