如何将可观察集合中的数据集更新为可观察集合中更新的值

发布于 2024-10-09 16:36:26 字数 854 浏览 6 评论 0原文

我目前正在从事 POS 和 POS 工作。库存应用程序,我正在将产品和库存可用数据从数据库加载到数据集,然后将数据集加载到可观察集合。最后,可观察集合绑定到 UI 控件,例如数据网格和列表视图。

现在,我想更新用户使用 UI 控件在可观察集合中所做的更改 到数据集,然后到数据库文件(SDF)...请显示可能的编码,以在可观察集合更改时从可观察集合更新数据集

。本论坛中我们的一位朋友给出了一个将数据集转换为可观察集合的示例。我尝试过并且效果很好。

代码是这样的 1. Person 类保存我的 Person 数据:

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

然后使用 LINQ 填充我的 ObservableCollection:

var people = new ObservableCollection<Person>(
dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
    Id = p.Field<int>("Id"),
    Name = p.Field<string>("Name"),
    Age = p.Field<int>("Age")
}));

它运行良好,现在,进一步前进我想从可观察集合中更新数据集,因为 UI Datagrid 条目中的 ObseravableCollection 发生变化,

请向我解释一下可能的编码完成这个任务。

i am currently working on POS & Inventory Application , i am loading the products and stocks available data from the databse to dataset then dataset to obseravable collection . finally obseravable collection is binded to UI Controls like Data grid and List view.

Now , i want to update the changes made in observable collection by the user using UI Controls
to data set and then to database file (SDF) ... please , show the possible coding to update the dataset from observable collection as observable collection changes

One of our friend in this forum gave a example to convert dataset to obseravable collection . i tried that and worked well .

code goes like this
1. Person class to hold My Person data:

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Then used LINQ to populate My ObservableCollection:

var people = new ObservableCollection<Person>(
dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
    Id = p.Field<int>("Id"),
    Name = p.Field<string>("Name"),
    Age = p.Field<int>("Age")
}));

its working well, now, moving further i want to update back dataset from obseravable collection as ObseravableCollection changes in UI Datagrid entries changes

please explain me about possible coding to achieve this task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

墨小墨 2024-10-16 16:36:26

我看到两个选项,

首先你可以订阅 ObservableCollection 中每个对象的 PropertyChanged 事件,并订阅 ObservableCollection 的 CollectionChanged 事件以保持这两个数据结构同步

,其次,我认为这更容易实现,是创建未更改的属性,在类中添加、修改、删除枚举值,根据用户操作设置此属性,并在保存时将其转换为数据集

编辑

这是第二种方法的简单实现,

像这样创建枚举

public enum DataObjectState
{
  Unchanged,
  Added,
  Modified,
  Deleted
}

而不是将属性添加到您的像这样的类

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DataObjectState ObjectState{get;set;}
}

,之后您可以像您展示的那样创建 ObservableCollection 表单数据集

dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
    Id = p.Field<int>("Id"),
    Name = p.Field<string>("Name"),
    Age = p.Field<int>("Age"),
    ObjectState = ConvertRowStateToObjectState(p.RowState)
}));

从 observablecollection 转换为数据集

,并可以使用相反的查询编辑

private DataObjectState ConvertRowStateToObjectState(DataRowState dataRowState)
        {
            return (DataObjectState)Enum.Parse(typeof(DataObjectState), dataRowState.ToString());
        }

我也找不到使用 Linq 创建数据集的方法,但这里是一点黑客方法来做你想做的事

        DataTable personsTable = new DataTable("Person");
        personsTable.Columns.Add("Id", typeof(int));
        personsTable.Columns.Add("Name", typeof(string));
        personsTable.Columns.Add("Age", typeof(int));

        foreach (var item in collection)
        {
            DataRow row = personsTable.NewRow();
            row["Id"] = item.Id;
            row["Name"] = item.Name;
            row["Age"] = item.Age;
            personsTable.Rows.Add(row);
            if (item.ObjectState == DataObjectState.Added)
            {
                continue;
            }
            row.AcceptChanges();
            if (item.ObjectState == DataObjectState.Deleted)
            {                   
                row.Delete();
            }
            else if (item.ObjectState == DataObjectState.Modified)
            {
                row.BeginEdit();
                row.EndEdit();
            }
        }

I see two options

first you can subscribe to PropertyChanged event of every object in ObservableCollection, and to CollectionChanged event of ObservableCollection to keep this 2 data structures in synch

and second, which is more easy to implement I think, is to create property with unchanged,added, modified,deleted enum vales in your class, set this property depending user actions, and convert this to DataSet when saving

EDIT

Here is a simple implementation for second method

create enum like this

public enum DataObjectState
{
  Unchanged,
  Added,
  Modified,
  Deleted
}

Than add property to your class like this

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public DataObjectState ObjectState{get;set;}
}

after which you can create ObservableCollection form dataset like you show

dataset.Tables["Person"].AsEnumerable().Select(p => new Person
{
    Id = p.Field<int>("Id"),
    Name = p.Field<string>("Name"),
    Age = p.Field<int>("Age"),
    ObjectState = ConvertRowStateToObjectState(p.RowState)
}));

and can convert from observablecollection to dataset by using opposite query

EDIT

private DataObjectState ConvertRowStateToObjectState(DataRowState dataRowState)
        {
            return (DataObjectState)Enum.Parse(typeof(DataObjectState), dataRowState.ToString());
        }

I doesn't find a way to create dataset with Linq too, but here is a bit hack method to do what you want

        DataTable personsTable = new DataTable("Person");
        personsTable.Columns.Add("Id", typeof(int));
        personsTable.Columns.Add("Name", typeof(string));
        personsTable.Columns.Add("Age", typeof(int));

        foreach (var item in collection)
        {
            DataRow row = personsTable.NewRow();
            row["Id"] = item.Id;
            row["Name"] = item.Name;
            row["Age"] = item.Age;
            personsTable.Rows.Add(row);
            if (item.ObjectState == DataObjectState.Added)
            {
                continue;
            }
            row.AcceptChanges();
            if (item.ObjectState == DataObjectState.Deleted)
            {                   
                row.Delete();
            }
            else if (item.ObjectState == DataObjectState.Modified)
            {
                row.BeginEdit();
                row.EndEdit();
            }
        }
世界如花海般美丽 2024-10-16 16:36:26

除了 @ArsenMkrt-s 解决方案之外,您还可以考虑使用强类型数据集,它将具有 PersonTable 类而不是可观察的 Collection 和 PersonRow 类而不是当前的 Person 类。

这些也可以绑定到数据网格。

好处:不需要转换代码。

Beside @ArsenMkrt-s solution you can also think of using a strong typed dataset that will have a PersonTable-class instead of the observable Collection and a PersonRow-class instead of your current Person-Class.

These can be bound to a datagrid, too.

The benefit: no conversion-code required.

苦笑流年记忆 2024-10-16 16:36:26

你可以使用过滤后的collectionviewsource来实现两个好的想法:
1-它不是一个新集合(内存需求较少)。
2-它就像表上的指针集合,具有两种方式的数据绑定功能。

you can use a filtered collectionviewsource for 2 good thinks :
1-it's not a new collection (less memory need).
2-it's like collection of pointers upon your Table with 2 ways databinding capability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文