This is a migrated thread and some comments may be shown as answers.

RadGrid Delete Edit and Create

3 Answers 244 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Yunier
Top achievements
Rank 1
Yunier asked on 24 Feb 2013, 09:02 PM
Hello everyone!!!

I am trying to show products data to a RadGrid, I have to create CRUD funtionalities, but the buttom to create a new product is outside the grid, and the link/buttoms to the edit and delete are in each row of the product entity. i am ussing Entityframework. 
Please if anyone can help me or giving me any ideao how I have to do it, the delete need to show a confirmation dialog.
Thanks and I hope you can give me some ideas.
Greetings!!!

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Feb 2013, 05:48 AM
Hi,

You can use FireCommand to invoke server events in external button click. Here is the sample code.
aspx:
<asp:LinkButton ID="LinkButton22" OnClientClick="javascript:return confirm('Delete all selected customers?')"
runat="server" Text="Delete"></asp:LinkButton>

C#:
protected void Button2_Click(object sender, EventArgs e)
    {
      RadGrid grid = (this.FindControl("RadGrid1") as RadGrid);
     (grid.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem).FireCommandEvent(RadGrid.PerformInsertCommandName, string.Empty);
     (grid.MasterTableView.GetItems(GridItemType.EditFormItem)[0] as GridEditableItem).FireCommandEvent(RadGrid.UpdateCommandName, string.Empty);
    }

Also check the following demo which implements similar scenario.
Grid - Command Item

Thanks,
Shinu
0
Yunier
Top achievements
Rank 1
answered on 25 Feb 2013, 06:41 AM
Hi Shinu thanks for you fast answer.
What I have done is the following but I am not sure i am on trghe correcty way. Please if you can help me. here is the example.

.asp

    <telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid1" AllowFilteringByColumn="false" runat="server" GridLines="None" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnDeleteCommand="RadGrid1_DeleteCommand">
        <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
        <MasterTableView DataKeyNames="ProductID">
            <Columns>
                <telerik:GridBoundColumn DataField="ProductName" HeaderText="Product name" SortExpression="ProductName" UniqueName="ProductName">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="UnitPrice" HeaderText="Unit Price">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn AllowFiltering="false" UniqueName="EditColumn">
                    <ItemTemplate>
                        <asp:ImageButton ID="ButtonEdit" runat="server" CommandName="Edit" AlternateText="Edit" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridButtonColumn ButtonType="ImageButton" ConfirmText="Are you sure you want to delete?" ImageUrl="~/Images/Delete.png" CommandName="Delete"  Text="Click to delete" UniqueName="Delete">
                </telerik:GridButtonColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
        </ClientSettings>
    </telerik:RadGrid>



C# code



       protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
             var _db = new WingtipToys.Models.ProductContext();
            IQueryable<Product> query = _db.Products;
            RadGrid1.DataSource = query.ToList();
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Response.Redirect("ProductDetails.aspx?productID=" + e.CommandArgument);
            }
        }


        public void Delete(int id)
        {
            var _db = new WingtipToys.Models.ProductContext();
            Product product = _db.Products.Find(id);
            _db.Products.Remove(product);
            _db.SaveChanges();
            Response.Redirect("test.aspx");

        }

        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ProductID"].ToString();
            Delete(int.Parse(id));
            //RadGrid1.Rebind();

        }

    }       



The case of the edit accion dont go to the direction, appears in the same page when I click on edit, dont know why?
I am new with telerik components I really dont know how to do it. 
Just want in my grid columns to edit and delete( with confirmation) and in case of edit go to another page to edit the row, and in case of add, just add a new row. Seems very simple!!!

Thnkas!!!!!
0
Shinu
Top achievements
Rank 2
answered on 26 Feb 2013, 06:40 AM
Hi,

After inspecting your code I found that  you haven't attached ItemCommand event  in ASPX. Please make sure that you are attaching the event and its firing in server side on edit button click.

ASPX:
<telerik:RadGrid AutoGenerateColumns="false" ID="RadGrid1" onitemcommand="RadGrid1_ItemCommand1" AllowFilteringByColumn="false" runat="server" GridLines="None" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource" OnDeleteCommand="RadGrid1_DeleteCommand">

Thanks,
Shinu.
Tags
Grid
Asked by
Yunier
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Yunier
Top achievements
Rank 1
Share this question
or