【问题标题】:How to synchronize Database and DataGridView如何同步数据库和DataGridView
【发布时间】:2014-01-22 13:41:44
【问题描述】:

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

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

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

好吧,如果我将表绑定到我的DataGridView.DataSource,我可以看到数据库中的所有条目都已加载并正确显示。然后,如果我更改某些内容,我会发现自己面临同步问题。更改的单元格在数据库端没有更改。

为了保存我实现此方法的更改:

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");
    }
 }

有没有办法让整个数据库同步自动发生?就像自动提交更改一样。我想我将不得不在模型类上进行一些更改。目前,它没有实现任何接口,也没有继承任何东西。

这是类声明:

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

此外,我还没有找到“正确”更新我的DataGridView 的方法。我现在就是这样做的,但似乎并不总是有效。有没有更好的方法来做到这一点?

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

感谢您的帮助!

【问题讨论】:

    标签: c# sql-server winforms visual-studio-2010 datagridview


    【解决方案1】:

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

    所以将BindingSource的DataSource设置为表格,然后将DataGridView的DataSource设置为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);
    

    Here's a tutorial and a bit more in-depth info about the binding source object:

    Saving Data from Application to Database (msdn):

    Data Binding using LINQ to SQL in C#

    【讨论】:

    • 好的,所以我的一次尝试并没有错。但是我现在在哪里提交对 DataGridView 的更改?我是否也使用 bindingSource 执行此操作? PS:公司防火墙已屏蔽您发布的链接。
    • 是的。您基本上在绑定源上工作。顾名思义,这个对象将数据库的数据绑定到控件(本例中为datagridview)。
    • 您可以调用 dataAdapter.Update(dataTable) 来更新底层数据库。准确地说:bindingsource.EndEdit(); dataAdapter.Update(dataTable);以下是有关绑定源对象的教程和更深入的信息:codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
    • 好的。因为我使用System.Data.Linq 作为 OOR 命名空间,所以我不得不做一些不同的事情,但我仍然对它的工作方式不满意。为了保存它,我使用Linq.Table&lt;Visitor&gt;.Context.SubmitChanges() 方法。为了刷新我的视图,我使用Linq.Table&lt;Visitor&gt;.Context.Refresh(),然后使用 BindingSource 执行ResetBindings(false)。这会更新 BindingSource 和 DataGridView。
    • 也许这可以帮助您优化您的解决方案:c-sharpcorner.com/UploadFile/mahesh/dbinDlinq07022007080049AM/… 它考虑了 Linq 并展示了使用绑定数据库的技术。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多