C# DataGridView对新项的编程选择不会更新其他数据绑定控件

C# DataGridView对新项的编程选择不会更新其他数据绑定控件,c#,winforms,data-binding,datagridview,bindingsource,C#,Winforms,Data Binding,Datagridview,Bindingsource,我有一个DataGridView和许多控件(用于编辑)都绑定到BindingSource。一切正常-单击DataGridView中的条目会使绑定的编辑控件显示和编辑所选项目。我希望能够在DataGridView中自动选择新创建的项,编辑控件也绑定到新创建的数据。为此,我为DataGridView.RowsAdded实现了一个处理程序,如下所示: private void dataGridViewBeasts_RowsAdded(object sender, DataGridViewRowsAdd

我有一个DataGridView和许多控件(用于编辑)都绑定到BindingSource。一切正常-单击DataGridView中的条目会使绑定的编辑控件显示和编辑所选项目。我希望能够在DataGridView中自动选择新创建的项,编辑控件也绑定到新创建的数据。为此,我为DataGridView.RowsAdded实现了一个处理程序,如下所示:

private void dataGridViewBeasts_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    // Force newly created items to be selected
    dataGridViewBeasts.Rows[e.RowIndex].Selected = true;
}

这在表面上起作用,在DataGridView中选择新创建的项。但是,编辑控件始终引用在创建新项之前选定的项。我如何鼓励他们指向新选择的项目?

假设:

DataTable dt = theBindingSource.DataSource as DataTable;
dt.Rows.Add("New Row", "9000");

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
List<Example> list = theBindingSource.DataSource as List<Example>;
list.Add(new Example() { Foo = "New Row", Bar = "9000" });

// Reset the bindings.
dataGridView1.DataSource = null;
dataGridView1.DataSource = theBindingSource;

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
您正在将新行添加到基础
数据源
,而不是直接添加到
数据网格视图

结果:

DataTable dt = theBindingSource.DataSource as DataTable;
dt.Rows.Add("New Row", "9000");

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
List<Example> list = theBindingSource.DataSource as List<Example>;
list.Add(new Example() { Foo = "New Row", Bar = "9000" });

// Reset the bindings.
dataGridView1.DataSource = null;
dataGridView1.DataSource = theBindingSource;

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
这里遇到的问题是,所有编辑控件上的绑定都绑定到
DataGridView.CurrentRow
绑定项,这是一个
get
only属性,由行标题列中的箭头指示

更改
CurrentRow
在中进行了讨论

因此,它应该非常简单,只需将新添加行的
CurrentCell
设置为
Cell[0]
。除了

DataGridView.RowsAdded
事件中设置
CurrentCell
。从概念上讲,它是有效的-新行成为
CurrentRow
。但在该事件完成后,调试将显示
CurrentRow
立即重置为其先前的值。相反,在代码后设置
CurrentCell
,以添加新行。例如,当
BindingSource.DataSource
时:

是一个
数据表

DataTable dt = theBindingSource.DataSource as DataTable;
dt.Rows.Add("New Row", "9000");

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
List<Example> list = theBindingSource.DataSource as List<Example>;
list.Add(new Example() { Foo = "New Row", Bar = "9000" });

// Reset the bindings.
dataGridView1.DataSource = null;
dataGridView1.DataSource = theBindingSource;

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
列表

DataTable dt = theBindingSource.DataSource as DataTable;
dt.Rows.Add("New Row", "9000");

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
List<Example> list = theBindingSource.DataSource as List<Example>;
list.Add(new Example() { Foo = "New Row", Bar = "9000" });

// Reset the bindings.
dataGridView1.DataSource = null;
dataGridView1.DataSource = theBindingSource;

dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];
List List=bindingsource.DataSource作为列表;
添加(新示例(){Foo=“new Row”,Bar=“9000”});
//重置绑定。
dataGridView1.DataSource=null;
dataGridView1.DataSource=绑定源;
dataGridView1.CurrentCell=dataGridView1.Rows[dataGridView1.Rows.Count-1]。单元格[0];

您是如何对另一个事件进行编码的?您选择了clicked事件还是selectionchanged事件?后者也适用于第二种情况1.我不确定你指的是哪一种情况。新项是通过向绑定实体框架DbContext添加一个项来创建的,如果这样有帮助的话?感谢您在任何情况下的回复:)您写道:单击DataGridView中的条目会导致。。这听起来好像您已经对Click或MouseClick事件进行了编码。是吗?没有。这一切都是通过数据绑定和BindingSource的魔力实现的。据我所知(这可能是错误的,我还不熟悉.NET的数据绑定),BindingSource提供了“当前记录”,它通过DataSource属性绑定到DataGridView中的选定项。更新所选内容将更新当前记录,该记录将应用于绑定到同一BindingSource的所有控件—在本例中为编辑控件。已经遵循了大致相同的思路,但没有任何效果。不过,下面的答案看起来很有希望。当我知道它是否有效时,我会在这里发布。我很感激你的时间和关注。工作很有魅力。非常感谢。