如何同步数据库和DataGridView [英] How to synchronize Database and DataGridView

查看:257
本文介绍了如何同步数据库和DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图通过一个 DataGridView 同步一个数据库。到目前为止,我已经创建了一个数据模型类。此类包含与数据库匹配的多个属性。它们使用 System.Data中的 [Table] [Column] 属性进行映射.Linq.Mapping 命名空间。

I have been trying to synchronize a database trough a 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.

好的。所以我把 DataGridView 使用 DataSource -Property绑定到一个 DataContext 连接到数据库(MSSQL)。这个逻辑在单例类中实现,所以我可以确保这个 DataContext 的单个实例。

Ok. So I bound the DataGridView using the DataSource-Property to a DataContext connecting to the Database (MSSQL). This Logic is implemented in a singleton class, so I can assure there is a single instance of this DataContext.

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

如果我将表绑定到我的 DataGridView.DataSource 我可以从数据库中查看我所有的条目,并正确显示。那么如果我改变了一些我发现自己遇到的同步问题。更改的单元格在数据库方面没有更改。

Well, if I bind the table to my DataGridView.DataSource I can see all my entries from the database loaded and shown correctly. 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.

这是类声明:

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

此外,我还没有找到一种方法来更新我的 DataGridView 正确的,这是我现在如何做,但它似乎并不总是有效,有没有更好的方法呢? / p>

Further on, I have not found a way to update my DataGridView "correctly". 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;

感谢您的帮助!

推荐答案

您需要使用BindingSource对象。这将使DataTable与DataGridView保持同步。

You need to use the BindingSource object. This will keep your DataTable synchronized with the DataGridView.

因此,将BindingSource的DataSource设置为表,然后将DataGridView的DataSource设置为BindingSource。

So set the DataSource of the BindingSource to the table, then set the DataSource of the DataGridView to the BindingSource.

示例:

// 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;

要更新通常称为

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

以下是一个教程,并提供有关绑定源对象的更深入的信息:

保存从应用程序到数据库的数据(msdn):

C#中使用LINQ to SQL的数据绑定

这篇关于如何同步数据库和DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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