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

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

问题描述

我一直在试图同步数据库槽一个 DataGridView的。到目前为止,我已经创建了一个数据模型类。这个类包含与数据库的多个属性。他们使用映射 [表] [专栏] System.Data这属性.Linq.Mapping 命名空间。



确定。所以我必然使用 DataGridView的数据源,物业到的DataContext 连接数据库(MSSQL)。这个逻辑在类实现的,所以我可以保证有这个的DataContext


的单个实例

  this.m_context =新的DataContext(康涅狄格州); 
this.m_VisitorTable = m_context.GetTable<&参观者GT;();



好吧,如果我的表绑定到我的 DataGridView.DataSource 我可以看到加载并正确显示数据库中所有我的条目。然后,如果我改变的东西,我发现自己面临的同步问题。更改后的细胞并没有改变在数据库方面。



要救我实现这个方法的变化:




公共无效的SaveChanges( )

  {

{
//我不知道我在这里做。
VisitorLogic.Instance.m_VisitorTable.Context.SubmitChanges(System.Data.Linq.ConflictMode.Con
//我也想看看是否进行了更改,这样我就可以在关闭之前保存。
this.m_bChangesMade = FALSE;
}
赶上(异常前)
{
MessageBox.Show(无法保存。,错误);
}
}




有没有一种方法,使整个同步到数据库中自动发生?像的变化。我想我将不得不改变对模型类的东西。现在,它没有实现任何接口,也没有继承任何东西。


$ b自动提交$ b

这是类的声明:

  [表(NAME =tblVisitor)] 
公一流的观众

另外,我还没有找到一种方法来更新我的的DataGridView 正确的,这是我怎么做,现在,但似乎它并不总是工作。有没有更好的方式来做到这一点?

  //从数据库中检索
VisitorLogic.Instance.m_VisitorTable.Context.Refresh新数据(System.Data.Linq.RefreshMode.OverwriteCurrentValues​​,VisitorLogic.Instance.m_VisitorTable);

//设置数据源为'空'
this.dataGridView.DataSource = NULL;

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

//绑定新的数据源,希望它会显示新的数据。
this.dataGridView.DataSource = VisitorLogic.Instance.m_VisitorTable;
this.m_bChangesMade = FALSE;



感谢您的帮助!


解决方案

您需要使用的BindingSource对象。这将让您的DataTable的DataGridView的同步。



所以的BindingSource的数据源设置为表,然后设置在DataGridView到BindingSource的数据源。



例如:

  //的DataGridView 
DataGridView的DG =新的DataGridView();

//的BindingSource(用于同步表和网格)
的BindingSource BS =新的BindingSource();

//设置BindingSource的数据源的表
bs.DataSource =表;

//设置网格的DataSource
dg.DataSource = BS;

要更新底层数据库,你通常会叫

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



这里有一个教程,有点更深入的了解绑定源对象信息:



http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial



保存数据从(MSDN)应用数据库:



http://msdn.microsoft.com/en-us/library/ms171932%28VS.80%29.aspx



数据绑定使用LINQ在C#



http://www.c-sharpcorner.com/Uplo​​adFile/mahesh/dbinDlinq07022007080049AM/dbinDlinq.aspx


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.

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>();

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.

This is the class declaration:

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

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;

Thanks for your help!

解决方案

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

So set the DataSource of the BindingSource to the table, then set the DataSource of the DataGridView to the 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:

http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial

Saving Data from Application to Database (msdn):

http://msdn.microsoft.com/en-us/library/ms171932%28VS.80%29.aspx

Data Binding using LINQ to SQL in C#

http://www.c-sharpcorner.com/UploadFile/mahesh/dbinDlinq07022007080049AM/dbinDlinq.aspx

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

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