【问题标题】:MySQL / C# Commit DataGridView Changes on cell edit?MySQL / C# 在单元格编辑时提交 DataGridView 更改?
【发布时间】:2014-07-03 11:45:04
【问题描述】:

我有一个包含 5 个 dataGridViews 的表单,当您单击单元格时,我需要能够保存对视图的更改,类似于您在 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 (row r in dg1.SelectedRows){} 块,但我不确定到底该怎么做。

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

【问题讨论】:

  • 既然查询的是同一张表temporary_table,何不尝试在一个DataGridView中全部搞定?
  • 项目经理的决定,不是我的。但这不是问题。我可以轻松地写几行来刷新其他 dataGridViews。问题是当单元格失去焦点时更新基础表的代码。

标签: c# mysql datagridview


【解决方案1】:

您可以像这样使用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);
         }
    }

【讨论】:

  • 但是这个更新查询是什么?这就是我的全部问题。我可以正常访问活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 2011-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多