如何将可观察集合中的数据集更新为可观察集合中更新的值
我目前正在从事 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到两个选项,
首先你可以订阅 ObservableCollection 中每个对象的 PropertyChanged 事件,并订阅 ObservableCollection 的 CollectionChanged 事件以保持这两个数据结构同步
,其次,我认为这更容易实现,是创建未更改的属性,在类中添加、修改、删除枚举值,根据用户操作设置此属性,并在保存时将其转换为数据集
编辑
这是第二种方法的简单实现,
像这样创建枚举
而不是将属性添加到您的像这样的类
,之后您可以像您展示的那样创建 ObservableCollection 表单数据集
从 observablecollection 转换为数据集
,并可以使用相反的查询编辑
我也找不到使用 Linq 创建数据集的方法,但这里是一点黑客方法来做你想做的事
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
Than add property to your class like this
after which you can create ObservableCollection form dataset like you show
and can convert from observablecollection to dataset by using opposite query
EDIT
I doesn't find a way to create dataset with Linq too, but here is a bit hack method to do what you want
除了 @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.
你可以使用过滤后的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.