使用From Access DB文件刷新数据网格视图

本文关键字:数据 数据网 网格 视图 刷新 文件 From Access DB 使用 | 更新日期: 2023-09-27 18:15:14

所以,我有一个简单的应用程序,需要通过使用Visual Studio 2012中的DataGrid工具来查看,插入,更新和删除access db文件中的数据。在初始加载时,我的数据库加载良好,据我所知,它是一个绑定数据源:

我使用了一个名为lawyers的Access DB表,并创建了一个名为lawyerBindingSource的绑定数据源,该数据源附加在我的LawyerForm中的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)
    {
    }
}
}

使用From Access DB文件刷新数据网格视图

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

当你关闭应用程序时,更改会丢失?如果是,那可能是因为数据库又被覆盖了。

查看此线程以获取更多信息:为什么要"Copy if newer"?当文件更新时不复制文件?

您需要使用Update TableAdapter方法将数据保存在Db中而不是在您的网格中:

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