[英]MySQL / C# Commit DataGridView Changes on cell edit?
I have a Form with 5 dataGridViews, and I need to be able to save the changes to the Views when you click into a cell, similar to how you would work in Excel. 我有一个包含5个dataGridViews的Form,并且需要能够在单击单元格时将对视图所做的更改保存到View,类似于在Excel中的工作方式。
Here is my connection info: public static MySqlConnection db = new MySqlConnection(); public string jobnumber = ""; public string str = "SERVER=192.168.1.149; DATABASE=starflitesystems; UID=iuapp; " + "Password=iuapp";
这是我的连接信息: public static MySqlConnection db = new MySqlConnection(); public string jobnumber = ""; public string str = "SERVER=192.168.1.149; DATABASE=starflitesystems; UID=iuapp; " + "Password=iuapp";
public static MySqlConnection db = new MySqlConnection(); public string jobnumber = ""; public string str = "SERVER=192.168.1.149; DATABASE=starflitesystems; UID=iuapp; " + "Password=iuapp";
My Form Open Code: 我的表格公开代码:
public frmPricingTemplate_Start(string s)
{
InitializeComponent();
createTempTable();
setDGVQueries();
hidePanels(pnlBasePackage);
btnReset(btnBasePackage);
this.WindowState = FormWindowState.Maximized;
jobnumber = s;
txtJobNumber.Text = s;
comboBox1.Visible = false;
comboBox2.Visible = false;
dataGridRefresh();
dg2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dg3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dg4.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dg5.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
My Database Queries: 我的数据库查询:
public void setDGVQueries()
{
/* Strings for dataGridViews */
selectDGV1 = "SELECT `Group`, Material, `Sub-Material` " +
"FROM temporary_table " +
"WHERE tab='" + activeTab + "';";
selectDGV2 = "SELECT Quantity as `Quantity`, Cost as `Cost` " +
"FROM temporary_table " +
"WHERE tab='" + activeTab + "';";
selectDGV3 = "SELECT Quantity2 as `Quantity`, Cost2 as `Cost` " +
"FROM temporary_table " +
"WHERE tab='" + activeTab + "';";
selectDGV4 = "SELECT Quantity3 as `Quantity`, Cost3 as `Cost` " +
"FROM temporary_table " +
"WHERE tab='" + activeTab + "';";
selectDGV5 = "SELECT Quantity as `Quantity`, Cost as `Cost` " +
"FROM temporary_table " +
"WHERE tab='" + activeTab + "';";
}
My Database Refresh Function: 我的数据库刷新功能:
public void dataGridRefresh()
{
/* Define all dataTables for the dataGridViews */
MySqlDataAdapter return1 = new MySqlDataAdapter(selectDGV1, str);
DataTable dt1 = new DataTable("base");
return1.Fill(dt1);
MySqlDataAdapter return2 = new MySqlDataAdapter(selectDGV2, str);
DataTable dt2 = new DataTable("base");
return2.Fill(dt2);
MySqlDataAdapter return3 = new MySqlDataAdapter(selectDGV3, str);
DataTable dt3 = new DataTable("base");
return3.Fill(dt3);
MySqlDataAdapter return4 = new MySqlDataAdapter(selectDGV4, str);
DataTable dt4 = new DataTable("base");
return4.Fill(dt4);
MySqlDataAdapter return5 = new MySqlDataAdapter(selectDGV5, str);
DataTable dt5 = new DataTable("base");
return5.Fill(dt5);
/* Set DataSources for all datagridViews */
dg1.DataSource = dt1;
dg2.DataSource = dt2;
dg3.DataSource = dt3;
dg4.DataSource = dt4;
dg5.DataSource = dt5;
}
Basically, this loads certain information into my dataGridViews, but now, after that data is loaded, I need to be able to click into a cell, edit it, and have it automatically commit that change to the table once the focus leaves the cell. 基本上,这会将某些信息加载到我的dataGridViews中,但是现在,在加载数据之后,我需要能够单击一个单元格,对其进行编辑,并在焦点离开该单元格后自动将更改提交到表中。
I looked up a few things online about it, but they all appear to have used a different method to connect to the db, so I don't think they will work for me. 我在网上查找了一些有关它的内容,但是它们似乎都使用了不同的方法来连接数据库,因此我认为它们不会对我有用。
My theory is that I need to use a `for each (row r in dg1.SelectedRows){} Block, but I am unsure of exactly what to do. 我的理论是我需要为每个块使用d(在dg1.SelectedRows中的行r){},但是我不确定该怎么做。
Any and all help is greatly appreciated. 任何帮助都将不胜感激。
You can use the DataGridView.CellEnd Edit Event like this: 您可以像这样使用DataGridView.CellEnd Edit事件:
private void Form_Load(object sender, EventArgs e)
{
myDataDridView.CellEndEdit += new DataGridViewCellEventHandler(myDataDridView_CellEndEdit);
}
and then: 接着:
private void myDataDridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
// Your update query here
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.