[英]MySQL updates from modified datagridview in c#
我有一个包含基于MySQL数据库中的数据集和tableadapter的过滤后的datagridview(隐藏标记为“已关闭”的条目)的表单。 我停留在涉及启动对话框表单的一部分上,该对话框包含基于同一数据集的另一个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的对话框时,整个数据集为null。 但是,有趣的是,当对话框关闭且未进行任何更改时,第一种形式的过滤后的数据网格仍然完好无损。 尝试了其他几种不同的方法,但都没有可行的结果。
我已经省略了设计师声明,但是如果需要澄清,我可以编辑问题。
我一定忽略了一些简单的事情。 这是正确的方法吗? 任何帮助表示赞赏。
我想通了...第一个dgv更像是一个开放记录的仪表板。 第二个dgv是存储在数据库中的所有文件的列表。 我希望能够“打开”和“关闭”记录,并且在仪表板上看不到“已关闭”的记录,但仍希望能够在某个时候将其备份并跟踪所有以前已关闭的记录。 我的问题是我试图对两个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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.