简体   繁体   中英

Refreshing DataGrid View With From Access DB File

So, I've got a simple application that needs to view, insert, update, and delete data from an access db file by using the DataGrid tool in Visual Studio 2012. Upon initial load, my database loaded fine, and to my knowledge it is a bound data source:

I used an Access DB table called lawyers and created a binding data source called lawyerBindingSource attached to my lawyerGridView inside of my LawyerForm.

The problem is that when I relaunch my application, the newly inserted data does not refresh within my GridView. I've read similar stackoverflow questions on this topic, but have yet to find a solution. Please help! Here is my form code:

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)
    {

    }
}
}

When the application is running, then the newly insterted/updated data is displayed correctly?

It's when you shutdown the application the changes are lost? If so, that's probably because the database is overwritten again.

Check this thread for more info: Why does "Copy if newer" not copy a file when the file is updated?

You need to use Update TableAdapter Method to save data in Db other than in your grid:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM