选择新添加的行 - DataGridView 和 BindingSource
我将一个新行添加到绑定到 DataGridView 的 BindingSource
source.AddNew();
之后,使用 BindingSource 获取新添加的行,并在排序后返回 DataGridView 中的下一行。
ROW "A"
ROW "B" <- myBindingSource.AddNew();
ROW "C"
myBindingSource.Current 给出 ROW“C”。 (它成为 DataGridView 中选定的行)
我需要这个,因为我想只更新新添加的行
DataRowView drv = (DataRowView)myBindingSource.Current;
myTableAdapter.Update(drv.Row);
而不是整个表。
myTableAdapter.Update(myDataSet.myTable);
另外,我想在插入后在 DataGridView 中选择这条新添加的行。
以某种方式有可能吗?
I'm adding a new Row to a BindingSource that is Bound to a DataGridView
source.AddNew();
After this, use BindingSource to get the newly added row is return the next row in the DataGridView when its sorted.
ROW "A"
ROW "B" <- myBindingSource.AddNew();
ROW "C"
myBindingSource.Current gives ROW "C". (it became the selected row in the DataGridView)
I need this because I want to update just the newly added row
DataRowView drv = (DataRowView)myBindingSource.Current;
myTableAdapter.Update(drv.Row);
and not the entire table.
myTableAdapter.Update(myDataSet.myTable);
and also, I would like to have this newly added line selected in the DataGridView after insertion.
is it possible in some way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 DataGridView 中的事件来执行此任务,如下所示:
将新添加的行标记为选定的行。
Use the events from the DataGridView like this for this task:
That marks the newly added row as the selected.
是否可以?我会说
是
。这是与之相关的文章:
DataGridView 和 BindingSource(在 Joel 的论坛上)
Is it possible? I would say
yes
.Here's an aricle related to it:
DataGridView and BindingSource (on Joel's Forum)
不知道它是最好的解决方案,但例如看起来比迭代更好。
Dont know id its the best solution but for instance looks better than iterate.
您已经确定了实现此目标的一种方法。另一种实现方法是完全忽略 UI:
当然,这会对表中所有添加的行(而不是刚刚添加的行)调用 Update,因此如果你有一些疯狂的场景,你有两种不同的方式向表中添加新行,但这是行不通的。
You've already identified one way to accomplish this. Another way to accomplish it is to ignore the UI completely:
Of course, this calls
Update
on all added rows in the table, not the one that was just added, so if you have some crazy scenario where you have two different ways of adding new rows to the table it won't work.从 Oliver Friedrich 的答案扩展,使用设计器中所示的控件属性创建的函数将如下所示:
Extending from Oliver Friedrich's answer, the function when created using the controls's property as shown in the designer will look like: