MySQL从c#中修改的datagridview更新

本文关键字:datagridview 更新 修改 MySQL | 更新日期: 2023-09-27 18:14:45

我有一个包含过滤的datagridview(隐藏标记为"closed"的条目)的表单,该表单基于来自MySQL数据库的数据集和tableadapter。我卡住了一个部分,涉及启动一个包含另一个基于相同数据集的datagridview的对话框表单,该数据集显示表中的所有条目。我希望能够用复选框列标记那些"关闭"的选择,单击"关闭这些记录"按钮,关闭该对话框,并将这些更改反映在过滤的datagridview中。

我已经尝试了多种方法来实现这个,但都没有运气。基本上,当我返回过滤后的datagridview....

时,最接近的尝试导致了一个空数据集。

我的过滤后的datagridview填充在这里:

this.dtClientTableAdapter.FillBy(this.DS.dtClient);

对话框在这里启动:

private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        CloseAgreement dlgCloseAgree = new CloseAgreement();
        dlgCloseAgree.ShowDialog();
        refreshRecords();
    }

未过滤的datagridview显示在对话框中,并填充在这里:

this.dtClientTableAdapter.Fill(this.DS.dtClient);

要设置更改,使用RowValidated事件:

private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
    {
        DataTable changes = ((DataTable)dataGridView1.DataSource).GetChanges();
        if (changes != null)
        {
            MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter);
            mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand();
            mySqlDataAdapter.Update(changes);
            ((DataTable)dataGridView1.DataSource).AcceptChanges();
        }         
    }

在对话框关闭之前,一切看起来都很正常。当返回到第一个表单中经过过滤的datagridview时,datagridview是空的,通过重新填充tableadapter来刷新是平稳的。在调试时,当关闭解释为空的datagridview的对话框时,整个数据集为空。有趣的是,当对话框关闭并且没有进行任何更改时,第一个表单中过滤后的数据网格仍然有效。其他几种不同的方法也尝试了,但都没有可行的结果。

我省略了设计者声明,但如果需要澄清,我可以编辑问题。

我一定是忽略了一些简单的东西。这是正确的方法吗?任何帮助都是感激的。

MySQL从c#中修改的datagridview更新

我明白了…第一个dgv更像是一个开放记录的仪表盘。第二个dgv是存储在数据库中的所有dgv的列表。我希望能够"打开"answers"关闭"记录,并且在仪表板中看不到"关闭"的记录,但仍然希望能够在某个时候重新打开它们,并跟踪所有先前关闭的记录。我的问题是,我试图使用相同的数据集为两个dgv的。一旦我创建了一个独立的数据集并查询数据库,我就能够进行更改、更新并返回到仪表板以查看更改。只是我犯了个大错误。这是我创建的第一个mysql数据驱动应用程序,所以我对mysql连接器进行了一些主要的学习。谢谢你的帮助。

在主DataGridView上过滤打开/关闭记录的另一种方法是提供一个筛选复选框,并简单地过滤数据。

下面是一个简单的例子:

using System;
using System.Data;
using System.Windows.Forms;
class Form1 : Form
{
    DataGridView dgv;
    CheckBox hideCloseCheckBox;
    BindingSource bindingSource;
    public Form1()
    {
        /*
         * Add a DataGridView with a simulated DataSet
         * 
         * Note that we add a BindingSource between the DataSource member of the DGV and the DataSet.
         * We will use that BindingSource to filter the items.
         */
        Controls.Add((dgv = new DataGridView
        {
            Dock = DockStyle.Fill,
            AutoGenerateColumns = false,
            Columns =
            {
                new DataGridViewTextBoxColumn { Name = "Name", DataPropertyName = "Name", HeaderText = "Name", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill },
                new DataGridViewCheckBoxColumn { Name = "Open", DataPropertyName = "Open", HeaderText = "Open", AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader },
            },
            DataSource = (bindingSource = new BindingSource(new DataSet
            {
                Tables =
                {
                    new DataTable("Table1")
                    {
                        Columns =
                        {
                            new DataColumn("Name", typeof(string)),
                            new DataColumn("Open", typeof(bool)),
                        },
                        Rows =
                        {
                            { "Item 1", true },
                            { "Item 2", false },
                            { "Item 3", true },
                            { "Item 4", false },
                            { "Item 5", true },
                        },
                    },
                },
            }, "Table1")),
        }));
        /*
         * Add a "Hide closed records" checkbox
         */
        Controls.Add((hideCloseCheckBox = new CheckBox
        {
            Dock = DockStyle.Top,
            Text = "Hide closed records",
            Padding = new Padding(20, 0, 0, 0),
        }));
        /*
         * When the user clicks that checkbox, we change the Filter on the BindingSource
         * 
         * See http://msdn.microsoft.com/en-us/library/cabd21yw.aspx for details on filter expressions.
         */
        hideCloseCheckBox.Click += (s, e) =>
            {
                if (hideCloseCheckBox.Checked)
                    bindingSource.Filter = "Open = true";
                else
                    bindingSource.Filter = "";
            };
        /*
         * The problem we have now is that when the user toggles the Open checkbox, the record doesn't
         * dissappear from view when the filter is enabled. This is because triggering the checkbox
         * doesn't change the row data until after the row is committed. This normally happens when
         * the user leaves the row. If you're allowing other editing on the row, this may be the 
         * desired behavior as you don't want the row to vanish mid-edit. However, if you do, you
         * can force the issue by causing a checkbox toggle to commit the row.
         */
        dgv.CellContentClick += (s, e) =>
            {
                // If the "Open" column is clicked
                if (dgv.Columns[e.ColumnIndex].Name == "Open")
                {
                    // Trigger EndEdit on the dgv and if that succeeds, trigger it on the bindingSource
                    if (dgv.EndEdit())
                        bindingSource.EndEdit();
                }
            };
    }
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}