【问题标题】:C# Updating Database Table with DataGridViewC# 使用 DataGridView 更新数据库表
【发布时间】:2017-06-26 05:54:06
【问题描述】:

给你的问题:

我正在使用 Visual Studio 2015 制作 Windows 窗体应用程序。目标只是能够使用数据网格查看器控件编辑 netezza 数据库中的简单表。

我将数据网格查看器控件放在我的表单上,创建了一个指向表的数据源,然后将数据网格视图指向该表。当我运行它并完美显示大约 10 行数据时,它运行良好。

有没有一种简单的方法可以从 datagridview 更新数据?我看到有编辑/锁定选项,但即使所有这些选项都设置得当,如果我在网格的单元格中编辑一个值并按 Enter,它不会返回并更新数据库。我需要为此手动编码吗?我不确定网格是否有办法自动完成。在代码中,我检查了 tableadapter 的所有方法,但没有找到 .Update 或类似的东西。

我们将不胜感激一些建议。谢谢!

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 SBTForceClose
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }
    private void LoadGrid()
    {
        dataGridView1.DataSource = dataSet1.LKP_SBT_FORCE_CLOSE;
        dataGridView1.Refresh();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LoadGrid();
    }

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

    }

    private DataRow LastDataRow = null;

    private void UpdateRowToDatabase()
    {
        if (LastDataRow != null)
        {
            if (LastDataRow.RowState == DataRowState.Modified)
            {
                this.lKP_SBT_FORCE_CLOSETableAdapter.Update(this.LastDataRow);
            }
        }

    }

    private void lKPSBTFORCECLOSEBindingSource_PositionChanged(object sender, EventArgs e)
    {
        BindingSource thisBindingSource = (BindingSource)sender;
        DataRow ThisDataRow = ((DataRowView)thisBindingSource.Current).Row;
        if (ThisDataRow == LastDataRow)
        {
            throw new ApplicationException();
        }
        UpdateRowToDatabase();
        LastDataRow = ThisDataRow;
    }
}
}

【问题讨论】:

    标签: c# database winforms visual-studio-2015


    【解决方案1】:

    最好的方法是创建一个按钮

        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
                conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BD\RepertoireDrapor.accdb";
                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "UPDATE Contact SET VAL1 = @VAL1, VAL2 = @VALE2 WHERE VAL3 = @VAL3";
                cmd.Parameters.AddWithValue("@VAL1", VAL1);
                cmd.Parameters.AddWithValue("@VAL2", VAL2);
                cmd.Parameters.AddWithValue("@VAL3", VAL3);
                cmd.Connection = conn;
                conn.Open();
                cmd.ExecuteNonQuery();
    

    在你的 datagridview 上你可以恢复像

    这样的值
     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
    
            dataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Red;
            rowselect = e.RowIndex;
            VAL3 = dataGridView1.Rows[rowselect].Cells[0].Value.ToString();
            VAL1 = dataGridView1.Rows[rowselect].Cells[1].Value.ToString();
            VAL2= dataGridView1.Rows[rowselect].Cells[2].Value.ToString();
    
        }
    

    希望对你有所帮助

    【讨论】:

      【解决方案2】:

      这可以通过使用DataAdapter 来实现。您可以在Updating Data Sources with DataAdapters 找到文档。

      Update database with changes made to DataTable… confusion 也有一个简短的示例。

      可以在IBM找到可能的驱动程序。

      【讨论】:

        【解决方案3】:

        没有内置的解决方案可以解决您想要完成的任务。而且您必须编写几行代码才能使其工作。这是一个链接,将引导您完成整个过程:

        https://www.codeproject.com/Articles/12846/Auto-Saving-DataGridView-Rows-to-a-SQL-Server-Data

        无论如何,恕我直言,我不建议在用户更改 DataGridView 上的值时访问数据库,因为每次用户更改某些内容并通过控件时,它都会调用数据库。这在小型应用程序中可能不是问题,但是随着您的应用程序的增长,我更喜欢控制这些事情。我找到了一种更好的方法,它有一个保存按钮或在输入过程完成时触发一个事件,并在该实例上执行我的数据库工作。

        【讨论】:

        • 所以,我遵循了这个指南,它没有抛出任何错误,但是它不会更新数据库。我刚刚更新了我的帖子以获取表单的代码。我在整个过程中都放置了断点,当我在 datagridviewer 中编辑一个单元格时,它确实会转到 updatedaterowtodatabase 方法,但它不会更新表。难道我做错了什么?看起来一切正常,但它不会更新该死的表!太令人沮丧了!
        猜你喜欢
        • 1970-01-01
        • 2016-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 1970-01-01
        相关资源
        最近更新 更多