[英]Databinding in Winforms
I have a data entry Winforms App in VS2010. 我在VS2010中有一个数据输入Winforms App。 Along with an Entity Data Model (.edmx) that is connected to a SQL Compact Database. 以及连接到SQL Compact数据库的实体数据模型(.edmx)。 In this database I have a table Ticket that I am trying to make an insert into. 在此数据库中,我有一张要插入的票证表。
On my form, I have all my controls DataBindings property setup and pointing to the correct fields in the database. 在我的表单上,我具有所有控件的DataBindings属性设置,并指向数据库中的正确字段。
I am trying to take the values from the controls on the form and add one non-databound field and save the ticket. 我正在尝试从表单上的控件中获取值,并添加一个非数据绑定字段并保存票证。
Here is my code: 这是我的代码:
private void btnSave_Click(object sender, EventArgs e)
{
this.TS = new TicketService();
Ticket t = (Ticket)ticketBindingSource.DataSource;
t.DateEntered = Datetime.Now;
TS.SaveTicket(t);
}
I am getting an error with Converting the TicketBindingSource to the Ticket Object. 将TicketBindingSource转换为票证对象时出现错误。
Anyone know how to do this conversion? 有人知道如何进行此转换吗?
Thanks 谢谢
Edit: I am not sure if I was clear enough on my issue, I do not believe this is uncommon. 编辑:我不确定我对我的问题是否足够清楚,我不认为这很常见。 All I am trying to do is take the data from my databound controls, place it into a variable, add a non-databound data (egtDateEntered = DateTime.Now like in the example above) and then Save the data to the database. 我要做的就是从数据绑定控件中获取数据,将其放入变量中,添加非数据绑定数据(例如上例中的egtDateEntered = DateTime.Now),然后将数据保存到数据库中。
I have done this in VB.Net, but VB.Net does this conversion from the DataSource to the Variable automatically. 我已经在VB.Net中完成了此操作,但是VB.Net会自动完成从数据源到变量的转换。 Here is the VB.Net code: 这是VB.Net代码:
Dim t = ticketBindingSource.DataSource
t.DateEntered = DateTime.Now()
TicketService.SaveTicket(t)
C# does not do this automatically, it throws an error. C#不会自动执行此操作,它会引发错误。
I'm not sure about your problem but these two lines do not make sense: 我不确定您的问题,但是这两行没有意义:
Ticket t = new Ticket();
t = (Ticket)ticketBindingSource.DataSource;
You create a Ticket and assign it to t. 您创建一个票证并将其分配给t。
Then, you immediately re-assign t to something else. 然后,您立即将t重新分配给其他内容。 The Ticket you created is never used and gets garbage collected. 您创建的票证从未使用过,并会被垃圾回收。
I don't know what do you want exactly, but this is a very good example: 我不知道您到底想要什么,但这是一个很好的例子:
Insert, Update and Delete using Entity Framework 使用实体框架进行插入,更新和删除
I hope help you. 希望对您有帮助。
Happy code ! 快乐的代码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.