检查 CollectionViewSource 中的行是否是新行
我有一个 CollectionViewSource (cvs),它以强类型 DataTable 作为源。 Cvs.View 设置为 DataGrid 的 ItemsSource。我想根据 DataGrid 中的更改从数据库中更新、插入和删除数据。我已经成功完成更新,并且我有删除的想法,但是对于插入我有一些问题。我尝试通过处理 cvs.View 的 CurrentChanging 事件来做到这一点,但行状态始终是分离的,应该添加它。这是我的代码:
private void View_CurrentChanging(object sender, CurrentChangingEventArgs e)
{
if (cvs.View.CurrentItem != null)
{
var dataRow = ((cvs.View.CurrentItem as DataRowView).Row) as MyDataSet.MyTableRow;
if (dataRow.HasChanges())
{
//do update - works
}
dataRow.EndEdit(); // without this line RowState is Unchanged when it should be Added
if (dataRow.RowState == DataRowState.Added)
{
//do insert - never goes here, RowState is Detached when it should be Added
}
}
}
这是正确的方法吗?我错过了什么吗?提前致谢。
编辑:DataGrid 绑定:
dataGrid1.ItemsSource = cvs.View;
I have a CollectionViewSource (cvs) which has strongly typed DataTable as it's source. Cvs.View is set as DataGrid's ItemsSource. I want to update, insert and delete data from a database based on changes in DataGrid. I have successfully done update, and i have an idea for delete, but for insert i have some problems. I tried to do it by handling CurrentChanging event of cvs.View but row state is always Detached and it should be Added. Here is my code:
private void View_CurrentChanging(object sender, CurrentChangingEventArgs e)
{
if (cvs.View.CurrentItem != null)
{
var dataRow = ((cvs.View.CurrentItem as DataRowView).Row) as MyDataSet.MyTableRow;
if (dataRow.HasChanges())
{
//do update - works
}
dataRow.EndEdit(); // without this line RowState is Unchanged when it should be Added
if (dataRow.RowState == DataRowState.Added)
{
//do insert - never goes here, RowState is Detached when it should be Added
}
}
}
Is this the right way to do it? Am I missing something? Thanks in advance.
EDIT: DataGrid binding:
dataGrid1.ItemsSource = cvs.View;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在我的wpf应用程序中使用以下内容:
只要您对数据表进行插入、更新或删除,它就会自动反映在您的视图/数据网格中。
中绑定到 DataGrid 的视图
编辑:MyView 是您在 UI XAML
i use this the following in my wpf app:
as far as you do an insert, update or delete to your DataTable its automatic reflected in your View/Datagrid.
EDIT: MyView is the View you bind to your DataGrid in your UI
XAML
这是一篇较旧的帖子,但我认为有另一个解决方案:
RowState 是我的控件中具有 DataRowState 类型的依赖属性。
正确初始化新行也是必要的!对我来说有效(我认为对其他人也有效):
希望这对某人有帮助!
It is an older post but I have another solution I think:
RowState is a dependency Property in my Control with type of DataRowState.
It is also neccessary to init the new Row correctly! For me works(And I think for others also):
Hope this helps somebody!
我遇到了同样奇怪的行为。如果数据行已分离并且您从 BindingSource.Item(即 datarowview)获取该行,则对该行调用 EndEdit 不会将该行添加到表中。所以作为一种解决方法。
首先检查数据行的 RowState,而不是调用 DataRow.EndEdit,如果分离,则手动将其添加到表中,即:-
I came across the same strange behavior. If a datarow is detached and you get the row from BindingSource.Item (i.e datarowview) then calling EndEdit to the row does not add the row to the table. So as a workaround.
Instead of calling DataRow.EndEdit first check the RowState of the data row, if detached then manually add it to the table i.e:-