从数据表中删除行 [英] Delete row from datatable

查看:53
本文介绍了从数据表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个gridview.我想从数据表中删除选定的gridview行.

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();


我想从数据表dt中删除此CustID.


I want to delete this CustID from a datatable dt.

推荐答案

您好,如果您使用某种DataContext,则可以使用以下内容.用数据上下文的名称替换"DataContext",用要删除的表的名称替换"TableName".

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
   }
}


HI
要删除数据表的行,只需找出行索引,然后使用此方法

dt.row.RemoveAt(rowindex);

希望对您有帮助
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


这篇关于从数据表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆