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

[Solved] Removing rows from RadGrid progmatically in server side

2 Answers 388 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 23 Sep 2009, 04:18 PM
Hello All,

I have a RadGrid containing CheckBox Columns which will indicate that RadGrids row will be deleted.
User selectes rows that she wants to delete using Checkboxes and then presses Button.
Button's Click handler is called and a method checks what rows are meant to be deleted.

I can get a reference to selected row's GridDataItem but I don't no how to delete it from RadGrid or RadGrid's Datasource.
I can't use any automatic insert/update/delete database techniques in this case.

Thanks,

2 Answers, 1 is accepted

Sort by
0
Mr. Plinko
Top achievements
Rank 1
answered on 23 Sep 2009, 05:25 PM
You can delete at database level with a query.

Specifically:

   protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)    
   {    
       //Get the GridDataItem of the RadGrid      
       GridDataItem item = (GridDataItem)e.Item;    
       //Get the primary key value using the DataKeyValue.      
       string EmployeeID = item.OwnerTableView.DataKeyValues[item.ItemIndex]["EmployeeID"].ToString();    
       try    
       {    
           //Open the SqlConnection      
           SqlConnection.Open();    
           string deleteQuery = "DELETE from Employees where EmployeeID='" + EmployeeID + "'";    
           SqlCommand.CommandText = deleteQuery;    
           SqlCommand.Connection = SqlConnection;    
           SqlCommand.ExecuteNonQuery();    
           //Close the SqlConnection      
           SqlConnection.Close();    
  
       }    
       catch (Exception ex)    
       {    
           RadGrid1.Controls.Add(new LiteralControl("Unable to delete Employee. Reason: " + ex.Message));    
           e.Canceled = true;    
       }      
  
    

If you throw a foreach in there, you can have it delete your selected columns.

Helpful?
0
Mike
Top achievements
Rank 1
answered on 24 Sep 2009, 06:08 AM
Hello,

As I mentioned Database actions are not suitable in this case because we are using Message/WCF based data manipulation in Grids.
Aren't there any way to delete a row fro DataSource?

Cheers,

Michael
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Mr. Plinko
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Share this question
or