简体   繁体   English

如何同步Database和DataGridView

[英]How to synchronize Database and DataGridView

I have been trying to synchronize a database trough a DataGridView . 我一直在尝试通过DataGridView同步数据库。 So far I have created a data model class. 到目前为止,我已经创建了一个数据模型类。 This class contains multiple Properties that match the database. 此类包含与数据库匹配的多个属性。 They are mapped using the [Table] and [Column] attributes from the System.Data.Linq.Mapping namespace. 它们使用System.Data.Linq.Mapping命名空间中的[Table][Column]属性进行映射。

Ok. 好。 So I bound the DataGridView using the DataSource -Property to a DataContext connecting to the Database (MSSQL). 所以我使用DataSource -Property将DataGridView绑定到连接到数据库(MSSQL)的DataContext This Logic is implemented in a singleton class, so I can assure there is a single instance of this DataContext . 这个逻辑是在单例类中实现的,所以我可以保证这个DataContext有一个实例。

 this.m_context = new DataContext(conn);
 this.m_VisitorTable = m_context.GetTable<Visitor>();

Well, if I bind the table to my DataGridView.DataSource I can see all my entries from the database loaded and shown correctly. 好吧,如果我将表绑定到DataGridView.DataSource我可以看到数据库中的所有条目都已加载并正确显示。 Then if I change something I found myself confronted with the synchronization problem. 然后,如果我改变了一些东西,我发现自己面临同步问题。 The changed cell did not change on the database side. 更改的单元格在数据库端没有更改。

To save the changes I implemented this method: 为了保存更改,我实现了这个方法:

public void SaveChanges()
{
    try
    {
       // I have no idea what I'm doing here.
       VisitorLogic.Instance.m_VisitorTable.Context.SubmitChanges(System.Data.Linq.ConflictMode.Con
       // I'm also trying to see if changes were made so I can save them before closing.
       this.m_bChangesMade = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Failed to save.", "Error");
    }
 }

Is there a way to make the whole synchronizing to the database happen automatically? 有没有办法让数据库的整个同步自动发生? Like autocommit on change. 就像改变自动提交一样。 I guess I will have to change something on the model Class. 我想我将不得不改变模型类的东西。 Right now, it does not implement any interfaces nor inherit anything. 现在,它没有实现任何接口也没有继承任何东西。

This is the class declaration: 这是类声明:

[Table(Name = "tblVisitor")]
public class Visitor

Further on, I have not found a way to update my DataGridView "correctly". 此外,我还没有找到一种方法来“正确”更新我的DataGridView This is how I make it now, but it seems it's not always working. 这就是我现在制作它的方式,但它似乎并不总是有效。 Is there a better way to do this? 有一个更好的方法吗?

// Retrieve the new data from the database
VisitorLogic.Instance.m_VisitorTable.Context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, VisitorLogic.Instance.m_VisitorTable);


// Set the DataSource to 'null'
this.dataGridView.DataSource = null;

// Refresh (?!)
this.dataGridView.Refresh();

// Bind the new DataSource, hoping it will show the new data.
this.dataGridView.DataSource = VisitorLogic.Instance.m_VisitorTable;
this.m_bChangesMade = false;

Thanks for your help! 谢谢你的帮助!

You need to use the BindingSource object. 您需要使用BindingSource对象。 This will keep your DataTable synchronized with the DataGridView. 这将使您的DataTable与DataGridView保持同步。

So set the DataSource of the BindingSource to the table, then set the DataSource of the DataGridView to the BindingSource. 因此,将BindingSource的DataSource设置为表,然后将DataGridView的DataSource设置为BindingSource。

Example: 例:

// DataGridView
DataGridView dg = new DataGridView();

// BindingSource (used for synchronizing table and grid)
BindingSource bs = new BindingSource();

// Set DataSource of BindingSource to table
bs.DataSource = table;

// Set grid DataSource
dg.DataSource = bs;

To update the underlying database you would usually call 要更新您通常会调用的基础数据库

bindingsource.EndEdit();
dataAdapter.Update(dataTable);

Here's a tutorial and a bit more in-depth info about the binding source object: 这是一个关于绑定源对象的教程和更深入的信息:

Saving Data from Application to Database (msdn): 将数据从应用程序保存到数据库(msdn):

Data Binding using LINQ to SQL in C# C#中使用LINQ to SQL进行数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM