使用Access数据库文件刷新DataGrid视图

时间:2014-07-26 18:19:53

标签: c# winforms visual-studio-2012 datagrid refresh

因此,我有一个简单的应用程序需要使用Visual Studio 2012中的DataGrid工具查看,插入,更新和删除访问数据库文件中的数据。初始加载时,我的数据库加载正常,据我所知,它是一个绑定的数据源:

我使用了名为lawyers的Access数据库表,并在我的LawyerForm内部创建了一个名为lawyerBindingSource的绑定数据源,附加到我的lawyerGridView。

问题是当我重新启动我的应用程序时,新插入的数据不会在我的GridView中刷新。我已经阅读过关于此主题的类似stackoverflow问题,但还没有找到解决方案。请帮忙!这是我的表单代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewcomerAndAssociatesSystem
{
public partial class Lawyers : Form
{
    public Lawyers()
    {
        InitializeComponent();
    }

    private void Lawyers_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'seniorProjectDb1DataSet.LawOffice' table. You can move, or remove it, as needed.
        this.lawOfficeTableAdapter.Fill(this.seniorProjectDb1DataSet.LawOffice);
        // TODO: This line of code loads data into the 'seniorProjectDb1DataSet.Lawyer' table. You can move, or remove it, as needed.
        this.lawyerTableAdapter.Fill(this.seniorProjectDb1DataSet.Lawyer);

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void lawyerReturnMain_Click(object sender, EventArgs e)
    {
        new MainForm().Show();
        this.Hide();
    }

    private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void fillByToolStripButton_Click(object sender, EventArgs e)
    {
        try
        {
            this.lawyerTableAdapter.FillBy(this.seniorProjectDb1DataSet.Lawyer);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }

    }

    private void fillByToolStripButton1_Click(object sender, EventArgs e)
    {
        try
        {
            this.lawOfficeTableAdapter.FillBy(this.seniorProjectDb1DataSet.LawOffice);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }

    }

    private void lawyersQueryToolStripButton_Click(object sender, EventArgs e)
    {
        try
        {
            this.lawyerTableAdapter.lawyersQuery(this.seniorProjectDb1DataSet.Lawyer);
        }
        catch (System.Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }

    }

    private void lawyerBindingSource_CurrentChanged(object sender, EventArgs e)
    {

    }
}
}

2 个答案:

答案 0 :(得分:0)

当应用程序运行时,新显示/更新的数据是否正确显示?

关闭应用程序时,更改会丢失吗? 如果是这样,那可能是因为数据库再次被覆盖。

检查此主题以获取更多信息: Why does "Copy if newer" not copy a file when the file is updated?

答案 1 :(得分:0)

您需要使用Update TableAdapter方法将数据保存在除网格之外的Db中:

 this.lawOfficeTableAdapter.Update(this.seniorProjectDb1DataSet.LawOffice);
 this.lawyerTableAdapter.Update(this.seniorProjectDb1DataSet.Lawyer);