【问题标题】:Updating DatagridView with DataGridViewCheckBoxColumn使用 DataGridViewCheckBoxColumn 更新 DatagridView
【发布时间】:2019-10-01 10:00:10
【问题描述】:

我想使用 DataGridViewCheckBoxColumns 从 DatagridView 更新 mySQL 表,并将其显示回我的表单。但是,即使只检查了一个 CheckBoxColumn,我当前的代码也会检查所有行。选中或未选中的行都应该更新,所以我不确定如果选中 DataGridViewCheckBoxColumns 是否还需要添加条件。

foreach (DataGridViewRow row in datagridFamMedHistory.Rows)          
{                
    command.Parameters.Clear();
    famMedHistorytype = datagridFamMedHistory.Rows[0].Cells["colType"].Value.ToString();
    command.CommandText = @"UPDATE familymedhistory SET paternal = @paternal, maternal = @maternal
                                    WHERE patientid = '" + patientid + "' AND type = '" + famMedHistorytype + "'";
    command.Parameters.AddWithValue("@paternal", row.Cells["colPaternal"].Value);
    command.Parameters.AddWithValue("@maternal", row.Cells["colMaternal"].Value);
    connect.Open();
    command.ExecuteNonQuery();
    connect.Close();
}

我想我错过了一些东西,但我似乎无法弄清楚。

【问题讨论】:

  • 打开和关闭数据库连接为每一行以及运行更新脚本为每一行是非常效率低下。考虑重新设计您的方法。
  • 这很好。

标签: c# mysql wpf datagridview


【解决方案1】:

您是否调试过您的代码?

您仍在循环中选择第 0 行

改变:

  famMedHistorytype = datagridFamMedHistory.Rows[0].Cells["colType"].Value.ToString();

到:

  famMedHistorytype = row.Cells["Column1"].Value.ToString();

也正如同事在评论中所写,不要在循环中打开和关闭连接..我的眼睛在流血

【讨论】:

  • 谢谢你,我喜欢听到这些类型的附加建议作为最佳实践。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多