C# 从绑定源绑定的类型化数据集中删除行的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/867349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Best way to delete a row from typed dataset bounded by binding source
提问by ant2009
C# 2008 SP1.
C# 2008 SP1。
I am deleting a row from a row that is currently selected on a datagridview.
我正在从当前在 datagridview 上选择的行中删除一行。
I am using a Typed dataset and my datagridview is bounded to a binding source.
我正在使用类型化数据集,并且我的 datagridview 绑定到绑定源。
However, I think my technique is not the best, even though it works.
然而,我认为我的技术不是最好的,即使它有效。
Many thanks for any advice,
非常感谢您的任何建议,
DataRow[] drDelete;
// Get the value of the PK from the currently selected row
DataRowView drv = (DataRowView)bsAddressBook.Current;
int index = Convert.ToInt16(drv[0]);
// Find the actual data row from the primary key and delete the row
drDelete = dsCATDialer.AddressBook.Select(string.Format("ID = '{0}'", index));
dsCATDialer.AddressBook.Rows.Remove(drDelete[0]);
回答by Henk Holterman
I think you can shorten this using the Row property of the DataRowView.
我认为您可以使用 DataRowView 的 Row 属性来缩短它。
// Get the value of the PK from the currently selected row
DataRowView drv = (DataRowView)bsAddressBook.Current;
DataRow drDelete = drv.Row;
dsCATDialer.AddressBook.Rows.Remove(drDelete);
回答by Thomas Levesque
You could also delete directly using the BindingSource :
您也可以使用 BindingSource 直接删除:
bsAddressBook.RemoveCurrent();