首页 > 解决方案 > DataGridView 显示来自 MS Access 数据库的表。如何在 bindingNavigator1 中编写保存按钮以更新数据库?

问题描述

我整天都在谷歌上搜索这个。我有一个使用数据库自​​动填充某些字段的表单,但是数据库中的数据需要能够定期更新。对我们来说最简单的方法是允许用户更新 datagridview 中的字段。我只是找不到如何将更改保存到数据库。提前感谢您提供任何提示,我是新手,所以一直在谷歌搜索,这是我无法找到任何明确答案的第一个问题。

    ```
    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 Calculator
    {
        public partial class Database : Form
        {
            public Database()
            {
                InitializeComponent();
            }

            private void Database_Load(object sender, EventArgs e)
            {
                // TODO: This line of code loads data into the 
    'tilesDataSet.Tiles_and_Quantities' table. You can move, or remove it, as needed.
                
    this.tiles_and_QuantitiesTableAdapter.Fill(this.tilesDataSet.Tiles_and_Quantities);

            }

            private void runtimeCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Hide();
                var form1 = new Form1();
                form1.Show();
            }

            private void standardCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Hide();
                var form3 = new Form3();
                form3.Show();
            }

            private void savedResultsToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Hide();
                var form2 = new Form2();
                form2.Show();
            }

            private void tiles_and_QuantitiesBindingNavigatorSaveItem_Click(object sender, 
    EventArgs e)
            {

            }

            private void tiles_and_QuantitiesDataGridView_CellContentClick(object sender, 
    DataGridViewCellEventArgs e)
            {

            }
        }
    }
    ```

我知道这有很多例子,但主要是使用 SQL,而且我使用的是 Access 数据库。仍然有一些例子表明了这一点,但我对此很陌生,并且正在努力实施它们。目前,我正在尝试:

    ```
     private void saveToolStripButton_Click(object sender, EventArgs e)
            {
                try
                {
                    this.Validate();
                    this.tiles_and_QuantitiesBindingSource.EndEdit();
                    this.tiles_and_QuantitiesTableAdapter.Update(this.tilesDataSet.Tiles_and_Quantities);
                    MessageBox.Show("Update successful");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Update failed");
    ```

并且在 .Update() 中收到错误 CS1061。再次,任何帮助将不胜感激。

标签: c#winforms

解决方案


推荐阅读