MySQL / C#Commit DataGridView对单元格编辑的更改?

时间:2014-07-03 11:45:05

标签: c# mysql datagridview

我有一个包含5个dataGridViews的Form,我需要能够在单击单元格时保存对Views的更改,类似于在Excel中的工作方式。

这是我的连接信息: public static MySqlConnection db = new MySqlConnection(); public string jobnumber = ""; public string str = "SERVER=192.168.1.149; DATABASE=starflitesystems; UID=iuapp; " + "Password=iuapp";

我的表格开放代码:

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;
}

我的数据库查询:

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 + "';";
}

我的数据库刷新功能:

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;
}

基本上,这会将某些信息加载到我的dataGridViews中,但现在,在加载数据之后,我需要能够单击一个单元格,对其进行编辑,并在焦点离开后让它自动将更改提交到表格中细胞。

我在网上查了几件关于它的内容,但他们似乎都使用了不同的方法来连接数据库,所以我认为它们不适合我。

我的理论是我需要使用`for each(dg1.SelectedRows中的row r){} Block,但我不确定该怎么做。

非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

您可以像这样使用DataGridView.CellEnd Edit事件:

private void Form_Load(object sender, EventArgs e)
    { 
         myDataDridView.CellEndEdit += new DataGridViewCellEventHandler(myDataDridView_CellEndEdit);
    }

然后:

private void myDataDridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
         try
         {
              // Your update query here
         }
         catch (Exception ex)
         {
              MessageBox.Show(ex.Message);
         }
    }