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

Delete from grid created in code behind

4 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adonis
Top achievements
Rank 1
Adonis asked on 14 Oct 2010, 06:11 PM
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?

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Oct 2010, 06:24 AM
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.
0
Adonis
Top achievements
Rank 1
answered on 15 Oct 2010, 05:23 PM
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 
 
0
Accepted
Marin
Telerik team
answered on 21 Oct 2010, 01:28 PM
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
0
dotnetrockerzzz
Top achievements
Rank 2
answered on 31 Oct 2010, 09:05 AM
Please look at my Code Problem  . It is similar but i m unable to solve It . Plz
Tags
Grid
Asked by
Adonis
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Adonis
Top achievements
Rank 1
Marin
Telerik team
dotnetrockerzzz
Top achievements
Rank 2
Share this question
or