选择新添加的行 - DataGridView 和 BindingSource

发布于 2024-08-09 22:59:12 字数 630 浏览 3 评论 0原文

我将一个新行添加到绑定到 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 技术交流群。

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

发布评论

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

评论(5

归途 2024-08-16 22:59:12

使用 DataGridView 中的事件来执行此任务,如下所示:

private void RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    this.Rows[e.RowIndex].Selected = true;
} 

将新添加的行标记为选定的行。

Use the events from the DataGridView like this for this task:

private void RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    this.Rows[e.RowIndex].Selected = true;
} 

That marks the newly added row as the selected.

戏蝶舞 2024-08-16 22:59:12

是否可以?我会说

这是与之相关的文章:
DataGridView 和 BindingSource(在 Joel 的论坛上)

Is it possible? I would say yes.

Here's an aricle related to it:
DataGridView and BindingSource (on Joel's Forum)

秋风の叶未落 2024-08-16 22:59:12

不知道它是最好的解决方案,但例如看起来比迭代更好。

        DataRowView drv = (DataRowView)source.AddNew();
        grupoTableAdapter.Update(drv.Row);
        grupoBindingSource.Position = grupoBindingSource.Find("ID", drv.Row.ItemArray[0]);

Dont know id its the best solution but for instance looks better than iterate.

        DataRowView drv = (DataRowView)source.AddNew();
        grupoTableAdapter.Update(drv.Row);
        grupoBindingSource.Position = grupoBindingSource.Find("ID", drv.Row.ItemArray[0]);
锦欢 2024-08-16 22:59:12

您已经确定了实现此目标的一种方法。另一种实现方法是完全忽略 UI:

foreach (DataRow r in myTable.AsEnumerable().Where(x => x.RowState == DataRowState.Added))
{
    myTableAdapter.Update(r);
}

当然,这会对表中所有添加的行(而不是刚刚添加的行)调用 Update,因此如果你有一些疯狂的场景,你有两种不同的方式向表中添加新行,但这是行不通的。

You've already identified one way to accomplish this. Another way to accomplish it is to ignore the UI completely:

foreach (DataRow r in myTable.AsEnumerable().Where(x => x.RowState == DataRowState.Added))
{
    myTableAdapter.Update(r);
}

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.

把回忆走一遍 2024-08-16 22:59:12

从 Oliver Friedrich 的答案扩展,使用设计器中所示的控件属性创建的函数将如下所示:

private void drv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    drv.Rows[e.RowIndex].Selected = true;
} 

Extending from Oliver Friedrich's answer, the function when created using the controls's property as shown in the designer will look like:

private void drv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    drv.Rows[e.RowIndex].Selected = true;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文