【发布时间】:2018-12-19 16:09:15
【问题描述】:
不知道为什么我弄错了。
试图在 datagridview 中选择行并将它们传递给存储过程。每次执行以下操作时,都会传递正确的行数,但例如,它将在第 1 行执行查询,而不是第 4 行。或者如果我选择了 2。如何将选定的行传递给查询,而不仅仅是选择的行数?
foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
SqlCommand sqlCmd = new SqlCommand("uspSelectWater", con);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@CaseNumberKey", this._CaseNum);
sqlCmd.Parameters.AddWithValue("@MasterAccount", dataGridView1.Rows[i].Cells[3].Value);
sqlCmd.Parameters.AddWithValue("@WaterAccount", dataGridView1.Rows[i].Cells[4].Value);
sqlCmd.Parameters.AddWithValue("@OwnerName", dataGridView1.Rows[i].Cells[5].Value);
sqlCmd.Parameters.AddWithValue("@MailName", dataGridView1.Rows[i].Cells[6].Value);
sqlCmd.Parameters.AddWithValue("@AcctBalance", dataGridView1.Rows[i].Cells[7].Value);
sqlCmd.ExecuteNonQuery();
}
}
【问题讨论】:
-
你不需要那个内循环。 A Row 有一个 Index 属性——你必须使用它。否则就是
r.Cells[3].Value等
标签: c# winforms stored-procedures datagridview