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.
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";
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.
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.
Any and all help is greatly appreciated.
You can use the DataGridView.CellEnd Edit Event like this:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.