检查 CollectionViewSource 中的行是否是新行

发布于 2024-11-19 22:10:43 字数 934 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

相思碎 2024-11-26 22:10:43

我在我的wpf应用程序中使用以下内容:

this.MyView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.MyDataTable);

只要您对数据表进行插入、更新或删除,它就会自动反映在您的视图/数据网格中。

中绑定到 DataGrid 的视图

private BindingListCollectionView _view;

public BindingListCollectionView MyView 
{
    get { return this._view; }
    protected set
    {
        this._view = value;
        this.NotifyPropertyChanged(() => this.MyView);
    }
}

编辑:MyView 是您在 UI XAML

<DataGrid ItemsSource="{Binding Path=MyView, Mode=OneWay, ValidatesOnDataErrors=true, ValidatesOnExceptions=true}" />

i use this the following in my wpf app:

this.MyView = (BindingListCollectionView)CollectionViewSource.GetDefaultView(this.MyDataTable);

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

private BindingListCollectionView _view;

public BindingListCollectionView MyView 
{
    get { return this._view; }
    protected set
    {
        this._view = value;
        this.NotifyPropertyChanged(() => this.MyView);
    }
}

XAML

<DataGrid ItemsSource="{Binding Path=MyView, Mode=OneWay, ValidatesOnDataErrors=true, ValidatesOnExceptions=true}" />
过度放纵 2024-11-26 22:10:43

这是一篇较旧的帖子,但我认为有另一个解决方案:

RowState="{Binding CurrentItem.Row.RowState}"

RowState 是我的控件中具有 DataRowState 类型的依赖属性。

正确初始化新行也是必要的!对我来说有效(我认为对其他人也有效):

DataRowView rv = dv.AddNew();
rv.EndEdit();

希望这对某人有帮助!

It is an older post but I have another solution I think:

RowState="{Binding CurrentItem.Row.RowState}"

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):

DataRowView rv = dv.AddNew();
rv.EndEdit();

Hope this helps somebody!

单调的奢华 2024-11-26 22:10:43

我遇到了同样奇怪的行为。如果数据行已分离并且您从 BindingSource.Item(即 datarowview)获取该行,则对该行调用 EndEdit 不会将该行添加到表中。所以作为一种解决方法。

首先检查数据行的 RowState,而不是调用 DataRow.EndEdit,如果分离,则手动将其添加到表中,即:-

If Row.RowState=DataRowState.Detached Then
  Row.Table.Rows.Add(Row)
Else
  Row.EndEdit
End If

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:-

If Row.RowState=DataRowState.Detached Then
  Row.Table.Rows.Add(Row)
Else
  Row.EndEdit
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文