Delete from grid created in code behind

5 posts, 1 answers
  1. Adonis
    Adonis avatar
    34 posts
    Member since:
    Nov 2008

    Posted 14 Oct 2010 Link to this post

       I am trying to delete from a grid that has been created entirely in code behind , how can I reference the grid to perform delete operations?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Oct 2010 Link to this post


    Use the FindControl(controlID) method to access teh dynamically created control.

    protected void Page_Load(object sender, EventArgs e)
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID =  "objRadGrid";
        RadGrid1.AllowPaging = true;
        RadGrid1.AutoGenerateColumns =false;
        RadGrid1.DataSourceID = "SqlDataSource1";
        RadGrid1.MasterTableView.AutoGenerateColumns = false;
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "CustomerID";
        boundColumn.UniqueName = "CustomerID";
        this.form1.Controls.Add(RadGrid1); // Adding the grid to controls collection
    protected void Button1_Click(object sender, EventArgs e)
        RadGrid grid = (RadGrid)this.Page.FindControl("objRadGrid"); // get the grid control
        grid.Visible = false;

  3. Adonis
    Adonis avatar
    34 posts
    Member since:
    Nov 2008

    Posted 15 Oct 2010 Link to this post

    Thanks for the response, let me clarify a bit I have a statically created grid , and a grid that is created dynamically in the code behind, I drag and drop setup between the static grid and the Dynamic grid , I am unable to determine how to delete a single item from the dynamic grid once it has been populated.

    I need to perform other operations upon delete so the client side delete button will not server the purpose. Is there a way to use OnDeleteCommand  via the code behind?

  4. Answer
    Marin avatar
    1057 posts

    Posted 21 Oct 2010 Link to this post

    Hi Adonis,

    You may programatically add GridButtonColumn and set its command name to "Delete". Then wire the OnDeleteCommand event for the grid and perform the delete operation. Here is a sample code on how to achieve this:
    GridButtonColumn deleteButtonColumn = new GridButtonColumn();
                deleteButtonColumn.ButtonType = GridButtonColumnType.LinkButton;
                deleteButtonColumn.Text = "Delete";
                deleteButtonColumn.UniqueName = "deleteButtonColumn";
                deleteButtonColumn.CommandName = "Delete";
                RadGrid1.DeleteCommand += new GridCommandEventHandler(RadGrid1_DeleteCommand);
    protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
                //peform delete operation

    If you have set your datasource to allow automatic operations (i.e. it has set Delete, Insert and Updates Commands). You can directly perform CRUD operations on items from the grid from code behind by using this methods:

    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. dotnetrockerzzz
    dotnetrockerzzz avatar
    32 posts
    Member since:
    Aug 2010

    Posted 31 Oct 2010 Link to this post

    Please look at my Code Problem  . It is similar but i m unable to solve It . Plz
Back to Top