C# 从绑定源限制的类型化数据集中删除行的最佳方法

发布于 2024-07-21 07:17:44 字数 632 浏览 12 评论 0原文

C# 2008 SP1。

我正在从 datagridview 上当前选择的行中删除一行。

我正在使用类型化数据集,并且我的 datagridview 绑定到绑定源。

然而,我认为我的技术虽然有效,但并不是最好的。

非常感谢您的任何建议,

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

C# 2008 SP1.

I am deleting a row from a row that is currently selected on a datagridview.

I am using a Typed dataset and my datagridview is bounded to a binding source.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2024-07-28 07:17:44

您还可以使用 BindingSource 直接删除:

bsAddressBook.RemoveCurrent();

You could also delete directly using the BindingSource :

bsAddressBook.RemoveCurrent();
祁梦 2024-07-28 07:17:44

我认为您可以使用 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);

I think you can shorten this using the Row property of the DataRowView.

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文