将更改从DataGridView(它的数据源是LINQ查询)提交到实际数据库 [英] Submit changes from DataGridView (which it's datasource is a LINQ query) to actual database

查看:84
本文介绍了将更改从DataGridView(它的数据源是LINQ查询)提交到实际数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

请假设我将LINQ查询绑定到DataGridView对象的DataSource属性,现在可以将用户更改(例如更新,删除和插入)提交到真实数据库了吗?
我使用了DataContext对象的SubmitChanges()方法,但无法正常工作!我的意思是用户更改不适用于数据库,并且当您再次加载数据时,用户更改将被丢弃!

(对不起,我的英语不好)

感谢您的关注!

Hi there !

Please assume I bind a LINQ query to DataSource property of a DataGridView object, now is it possible to submit users changes (like updates, deletes and inserts) to real database?
I used SubmitChanges() method of my DataContext object but it not work ! I mean user changes don''t apply on database, and when you load the data again, user changes will discards!

(excuse me for my bad English)

Thanks for your attention!

推荐答案

您可以在LinQ中进行操作.您甚至可以一次提交多个更改.

从网格视图事件中获取该行的键,该键可用于查询linQ对象.

例如,假设您在dataContext类中具有Products表.

1)实例化.

2)获取所需的产品LinQ对象.

3)分配更改

4)然后调用SubmitChanges()函数.

5)不要忘记绑定数据网格视图.

You can do in LinQ. You can even submit multiple changes at once.

From the grid view event get the key for the row, which can be used to query the linQ object.

For example assume you have Products table in your dataContext class.

1)Instantiate it.

2) Get Product LinQ object you want.

3) Assign the changes

4) then call SubmitChanges() function.

5) Don''t forgot to Bind the datagrid view.

DataClassesDataContext dc = new DataClassesDataContext(); //Data context object
Product product = dc.Products.Single(name => name.ProductName == "Product1"); // get  a single product
product.ProductName = "Product2";// assign a value
dc.SubmitChanges();// submit the changes
GridView1.DataBind();// bind the gridview



改进的答案
----------------------
根据您的评论,是的,这是可能的.

创建datacontext类后,

1)将linQ数据源添加/拖到您的页面.通过选择数据上下文对其进行配置.
它将显示诸如Enable Insert, Enable Update, Enable Delete之类的选项.选中这些框.

2)添加数据网格视图,并将数据源分配给linQ数据源.网格视图还显示了类似于上述启用的选项.检查这些值.

在运行时,将有编辑和删除链接.编辑将填充一个编辑模板.完成后,单击更新链接,它将自动更新到数据库.

无需编写代码即可手动处理更新和删除.

改进的Answer2
-------------------------

对于Windows应用程序,我认为我们需要显式调用Submit Changes函数.作为示例,向表单添加一个数据网格视图,一个绑定源和一个按钮.设置datagrid''s datasource to the binding source. Datagrid的允许编辑应为true.绑定源允许设置为true,自动生成成员也设置为true.假设数据上下文有一个Product对象,该对象映射到products表.为了提交更改,需要将datacontext对象维护为表单的全局变量.请遵循以下代码.



Improved Answer
----------------------
As per your comment, Yes it is possible automatically.

Once you created the datacontext classes

1) Add/ Drag a linQ datasource to your page. Configure it by selecting the datacontext.
It will show options like Enable Insert, Enable Update, Enable Delete. Check those boxes.

2) Add the data grid view and assign the datasource to the linQ datasource. The gridview also shows options like the above enables. Check those values.

At runtime there will be edit and delete link. Edit will populate an edit template. Once done click the update link it will be automatically update to the database.

No code need to be written to manually handle updates and deletes.

Improved Answer2
-------------------------

For windows application I think we need to explicitly call the Submit Changes function. As an example add a datagrid view, a binding source and a button to the form. Set the datagrid''s datasource to the binding source. Datagrid''s allow edit should be true. Binding sources allowables set true and autogenerate member also true. Assume the data context has a Product object which mapped to products table. The datacontext object need to be maintain as a global variable to the form in order to submit the changes. Follow the code below.

public partial class Form1 : Form
 {
     DataClasses1DataContext dc;
     public Form1()
     {
         InitializeComponent();
     }
     private void Form1_Load(object sender, EventArgs e)
     {
         dc = new DataClasses1DataContext();
         dataGridView1.AutoGenerateColumns = true;
         bindingSource1.DataSource = dc;
         bindingSource1.DataMember = "Products";

     }
     private void button1_Click(object sender, EventArgs e)
     {
         bindingSource1.EndEdit();
         dc.SubmitChanges();
     }
 }


这篇关于将更改从DataGridView(它的数据源是LINQ查询)提交到实际数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆