【问题标题】:How to access a databound DataGridView's DataSource?如何访问数据绑定 DataGridView 的数据源?
【发布时间】:2020-07-11 11:54:35
【问题描述】:

我正在使用 PostgreSQL 数据库在 C# 中构建一个数据驱动的 WinForms 应用程序。目前我正在填充我的两个 DataGridViews,如下所示。

我有一个课程,我计划在其中完成所有与数据库相关的工作 - 从数据库中获取数据,在数据库表中添加更新和删除数据等。该课程目前看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Npgsql;

namespace Test_XRef_Tool.Xref_Test
{
    public class Db
    {
        private string connString = String.Format("Server = localhost; Port = 5432; Database = dvdrental; User Id = userid; Password = password;");

        // Used to retrieve data for population of controls (DGVs, CBOs, etc)
        public DataTable GetData(string selectQuery)
        {
            NpgsqlConnection conn = new NpgsqlConnection(connString);
            DataSet ds = new DataSet();

            try
            {
                conn.Open();
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
                conn.Close();

                da.Fill(ds);
                return ds.Tables[0];
            }
        }

    }
}

在我的 Form1 类中,我填充了两个 DGV。填充它们的代码如下所示:

Db categoriesData = new Db();
dgvCategories.DataSource = categoriesData.GetData("SELECT * FROM actor");

Db defaultsData = new Db();
dgvDefaults.DataSource = defaultsData.GetData("SELECT * FROM film");

这一切都可以很好地填充 DGV,但我意识到在用户编辑 DGV 和我计划在程序中执行的各种其他操作期间,我需要访问 DGV 的数据源 - 在对 DGV 进行任何更改之前DGV 细胞。

我该怎么做?我的想法是我需要在 Form1 类上创建某种对象或变量来保存 DGV 数据的当前状态,但我不知道该怎么做——如果这甚至是正确的方法.

【问题讨论】:

  • dgvCategories.DataSource as DataTable。您可以拥有一个 DataTable 字段,也可以保留 DataAdapter,您可能会需要它(如果您设置了它的命令)。如果你需要绑定其他Controls,使用一个BindingSource作为你的DGV的DataSource和其他控件DataBindings的来源(BindingSource的DataSource当然是你的DataTable)。

标签: c# winforms datagridview datatable dataset


【解决方案1】:

根据 jimi 的评论,如果您想要 DGV 绑定到的数据表,它仍然存在,在 DGV 的数据源中:

DataTable dt = dgv.DataSource as DataTable;

如果您编辑存储在数据表中的值,数据表将记住原始值。想象一下,您将 John Smith 下载到数据表的第一行,其中有一列名为“name”:

Console.WriteLine(dt[0]["name"]); //prints John Smith

dt[0]["name"] = "Jane Doe";

该行现在有两个版本:

Console.WriteLine(dt[0]["name", DataRowVersion.Current]); //prints Jane Doe
Console.WriteLine(dt[0]["name", DataRowVersion.Original]); //prints John Smith 

如果您在数据行(或整个数据表)上接受更改,则原始将被当前覆盖:

dt.AcceptChanges();
Console.WriteLine(dt[0]["name", DataRowVersion.Current]); //prints Jane Doe
Console.WriteLine(dt[0]["name", DataRowVersion.Original]); //prints Jane Doe

ps;您可以在辅助方法中取消数据集

    public DataTable GetData(string selectQuery)
    {
        NpgsqlConnection conn = new NpgsqlConnection(connString);
        DataTable dt = new DataTable();

        try
        {
            conn.Open();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
            conn.Close();

            da.Fill(dt);
            return dt;
        }
    }

【讨论】:

    猜你喜欢
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多