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

    Hello, 
       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

    Hi,

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

    Code:
    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";
        RadGrid1.MasterTableView.Columns.Clear();
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        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;
    }



    -Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. 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?


    Thanks 
     
  5. Answer
    Marin
    Admin
    Marin avatar
    1045 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.MasterTableView.Columns.Add(deleteButtonColumn);
      
                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:
    RadGrid1.MasterTableView.PerformDelete(GridEditableItem),
    RadGrid1.MasterTableView.PerformInsert(),
    RadGrid1.MasterTableView.PerformUpdate(GridEditableItem).

    Regards,
    Marin
    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
  6. 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
UI for ASP.NET Ajax is Ready for VS 2017