MySql - 如何在更新回到wamp时跳过空行

时间:2017-03-17 16:50:47

标签: c# mysql datagridview foreach

我正在尝试创建一个按钮,将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的底部。这是不可能的,因为它是空的。如何让它跳过最后一个/空行?

2 个答案:

答案 0 :(得分:0)

我认为最后没有'

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

答案 1 :(得分:0)

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

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