Click here to Skip to main content
16,017,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a gridview. I want to delete the selected row of the gridview from a datatable.
CustID = customersGridView.DataKeys[rowItem.RowIndex]["Customer_ID"].ToString();

I want to delete this CustID from a datatable dt.
Posted
Updated 25-Nov-10 19:38pm
v2
Comments
JF2015 26-Nov-10 1:38am    
Edited to improve readability.

Hi there, if you're using a DataContext of some sort you can use the following. Substitute 'DataContext' with the name of your data context, and 'TableName' with the name of the table you're deleting from.

using System.Linq;

using (DataContext dc = new DataContext())
{
   // Retrieve the customer from the database using the ID
   var deletedCustomer = (from c in dc.TableName
                          where c.customer_id == CustID
                          select c).Single();

   try
   {
      // Sets the deletion of that customer to pending
      dc.TableName.DeleteOnSubmit(deletedCustomer);

      // Attempts to commit the change.
      dc.SubmitChanges();
   }
   catch (Exception ex)
   {
      // Your error handling
   }
}
 
Share this answer
 
Comments
m@dhu 26-Nov-10 4:23am    
Good call. But not sure whether op was using Linq to sql.
Dalek Dave 26-Nov-10 4:26am    
Good Answer.
HI
To delete the rowof the datatable just find out the row index then use this method

dt.row.RemoveAt(rowindex);

Hope this will help you
 
Share this answer
 
v2
Comments
Arun K V 26-Nov-10 4:20am    
not by rowindex..I want to delete the row by its datafield

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900