0

我有一个表单,其中包含基于 MySQL 数据库中的数据集和 tableadapter 的过滤数据网格视图(隐藏标记为“关闭”的条目)。我被困在一个涉及启动对话框表单的部分上,该表单包含另一个基于相同数据集的数据网格视图,该数据集显示表中的所有条目。我希望能够使用复选框列标记那些“关闭”的选择,单击“关闭这些记录”按钮,关闭该对话框并将这些更改反映在过滤的 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刷新是平安无事的。调试时,当关闭解释空数据网格视图的对话框时,整个数据集为空。有趣的是,当对话框关闭并且没有进行任何更改时,第一种形式的过滤数据网格仍然完好无损。尝试了其他几种不同的方法,但没有可行的结果。

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

我必须忽略一些简单的事情。这甚至是正确的方法吗?任何帮助表示赞赏。

4

2 回答 2

1

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

于 2013-05-23T15:10:12.940 回答
0

在主 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());
    }
}
于 2013-05-22T19:03:12.833 回答