【问题标题】:MySql - How do I skip empty rows when updating back to wampMySql - 更新回 wamp 时如何跳过空行
【发布时间】:2017-03-17 16:50:46
【问题描述】:

我正在尝试制作一个按钮,将在 datagridview 中输入的数据更新回我的 wampserver。

private void bt_form1_2_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in Dataview.Rows)
    {
        string sqlSelectAll = "UPDATE klant SET Klant_ID = '" + row.Cells["Klant_ID"].
            Value + "', Voornaam = '" + row.Cells["Voornaam"].
            Value + "', Achternaam = '" + row.Cells["Achternaam"].
            Value + "', Adres = '" + row.Cells["Adres"].
            Value + "', Woonplaats = '" + row.Cells["Woonplaats"].
            Value;

        MySqlCommand c = new MySqlCommand(sqlSelectAll, con);
        c.ExecuteNonQuery();
    }
}

但我不断收到错误,显然其原因是此代码试图将空行保存在 datagridview Picture 的底部。这是不可能的,因为它是空的。如何让它跳过最后/空行?

【问题讨论】:

  • 错误是什么?
  • Row.IsNewRow 将指示新行。从您的代码中可能是if (! row.isNewRow)
  • Furkan Kambay 提供的答案确实消除了错误。但是,它现在没有保存任何内容,重新启动后单元格仍然是空的。
  • 感谢 awnser JohnG,但不幸的是仍然遇到同样的错误。猜猜我的编码老师也不知道错误的来源,因为他说是导致错误的空行。

标签: c# mysql datagridview foreach


【解决方案1】:

我认为只是最后没有'。

... + "', Woonplaats = '" + row.Cells["Woonplaats"].
        Value + "';";

【讨论】:

    【解决方案2】:

    对下面的更改将捕获新行并跳过它。

    foreach (DataGridViewRow row in Dataview.Rows)
    {
      if (!row.IsNewRow) {
        string sqlSelectAll = "UPDATE klant SET Klant_ID = '" + row.Cells["Klant_ID"].
        Value + "', Voornaam = '" + row.Cells["Voornaam"].
        Value + "', Achternaam = '" + row.Cells["Achternaam"].
        Value + "', Adres = '" + row.Cells["Adres"].
        Value + "', Woonplaats = '" + row.Cells["Woonplaats"].Value;
    
        MySqlCommand c = new MySqlCommand(sqlSelectAll, con);
        c.ExecuteNonQuery();
      }
      else {
        // new row skipped
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多