C# 选择新添加的行 - DataGridView 和 BindingSource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1664537/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:48:08  来源:igfitidea点击:

Select newly added Row - DataGridView and BindingSource

c#winformsdatagridviewsql-server-cebindingsource

提问by Ruben Trancoso

I'm adding a new Row to a BindingSource that is Bound to a DataGridView

我正在向绑定到 DataGridView 的 BindingSource 添加一个新行

source.AddNew();

After this, use BindingSource to get the newly added row is return the next row in the DataGridView when its sorted.

在此之后,使用 BindingSource 获取新添加的行,并在排序时返回 DataGridView 中的下一行。

ROW "A"
ROW "B" <- myBindingSource.AddNew();
ROW "C"

myBindingSource.Current gives ROW "C". (it became the selected row in the DataGridView)

myBindingSource.Current 给出 ROW "C"。(它成为 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.

而且,我想在插入后在 DataGridView 中选择这个新添加的行。

is it possible in some way?

以某种方式可能吗?

采纳答案by Ruben Trancoso

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

回答by o.k.w

Is it possible? I would say yes.

是否可以?我会说yes

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

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

回答by Robert Rossney

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

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

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

Of course, this calls Updateon alladded 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.

当然,这需要Update所有的表,而不是刚刚添加的一个添加的行,所以如果你有一些疯狂的情况下,你必须增加新的行表中是行不通的两种不同的方式。

回答by Oliver Friedrich

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

像这样使用 DataGridView 中的事件来完成这个任务:

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

That marks the newly added row as the selected.

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

回答by Ganesh Kamath - 'Code Frenzy'

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

根据 Oliver Friedrich 的回答,使用设计器中显示的控件属性创建的函数将如下所示:

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