1

In the below code i have radgrid i want to insert, delete, update in the grid. But i don't know whether to get the values and place the code because i am new to telerik controls.

ASP:

<telerik:RadGrid ID="RadGrid1" runat="server"   AllowPaging="True" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
        AllowAutomaticDeletes="true" AllowSorting="true" OnItemCreated="RadGrid1_ItemCreated"
        OnItemInserted="RadGrid1_ItemInserted" OnPreRender="RadGrid1_PreRender"  >
            <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView AutoGenerateColumns="False"
            DataKeyNames="EmployeeTypeID" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="EmployeeTypeID" HeaderText="EmployeeTypeID" SortExpression="EmployeeTypeID"
                    UniqueName="EmployeeTypeID" Visible="false" >
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="EmployeeType" HeaderText="EmployeeType" SortExpression="EmployeeType"
                    UniqueName="EmployeeType">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CostPerDay" HeaderText="CostPerDay" SortExpression="CostPerDay"
                    UniqueName="CostPerDay">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="WorkingHours" HeaderText="WorkingHours" SortExpression="WorkingHours"
                    UniqueName="WorkingHours">
                </telerik:GridBoundColumn>

                <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />
            </Columns>
            <EditFormSettings>
                <EditColumn ButtonType="ImageButton" />
            </EditFormSettings>
        </MasterTableView>
            </telerik:RadGrid>

C#

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (!(e.Item is GridEditFormInsertItem))
                {
                    GridEditableItem item = e.Item as GridEditableItem;
                    GridEditManager manager = item.EditManager;
                    GridTextBoxColumnEditor editor = manager.GetColumnEditor("EmployeeTypeID") as GridTextBoxColumnEditor;
                    editor.TextBoxControl.Enabled = false;
                }
            }
        }
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {

                e.ExceptionHandled = true;
                SetMessage("Employee cannot be inserted. Reason: " + e.Exception.Message);

            }
            else
            {
                SetMessage("New Employee is inserted!");
            }
        }
        private void DisplayMessage(string text)
        {
            RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
        }

        private void SetMessage(string message)
        {
            gridMessage = message;
        }

        private string gridMessage = null;

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(gridMessage))
            {
                DisplayMessage(gridMessage);
            }
        }
2
  • I'm not sure I follow exactly what your problem is. Are you not seeing the edit fields? Is the delete button not showing up? Commented Nov 12, 2014 at 15:28
  • @paqogomez yes ic an see edit and delete but i don't know where to place edit code and delete code in code behind Commented Nov 12, 2014 at 17:08

2 Answers 2

1

You have the insert command handler in your ASPX, but you are missing the delete handler.

<telerik:RadGrid ID="RadGrid1" 
                 runat="server"   
                 AllowPaging="True" 
                 OnDeleteCommand="RadGrid1_DeleteCommand"
                 ...

The insertions and deletions to your grid happen automatically, but they dont happen automatically in your database.

protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
{
    //...ADD CODE TO INSERT TO YOUR DB HERE!
    if (e.Exception != null)
    {
     ...

Telerik's edit examples

protected void RadGrid1_DeleteCommand(object source, GridInsertedEventArgs e)
{
    //...ADD CODE TO DELETE FROM YOUR DB HERE!
    ...

Telerik's deleting example

Dont forget to rebind the grid to see the results. RadGrid1.DataBind();

Sign up to request clarification or add additional context in comments.

Comments

0

Another option is to define the update/delete/insert operations in the datasource (e.g., SqlDataSource control, it has the appropriate SQL commands as properties and they can have parameters) and use the built-in CRUD operations: http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/automatic-crud-operations/defaultcs.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.