C# 在Datagridview执行存储过程中选择行

C# 在Datagridview执行存储过程中选择行,c#,winforms,stored-procedures,datagridview,C#,Winforms,Stored Procedures,Datagridview,不知道我为什么会弄错 尝试在datagridview中选择行并将其传递给存储过程。每次执行以下操作时,都会传递正确的行数,但它会在第1行执行查询,而不是第4行。或者如果我选择了2。如何将所选行传递给查询,而不仅仅是所选行数 foreach (DataGridViewRow r in dataGridView1.SelectedRows) { for (int i = 0; i < dataGridView1.SelectedRows.Count; i++) {

不知道我为什么会弄错

尝试在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();
    }   
}
foreach(dataGridView1.SelectedRows中的DataGridViewRow r)
{
for(int i=0;i
使用类似的方法读取值,然后传递到存储过程:

int i;
i = dg.SelectedCells[0].RowIndex;
str1= dg.Rows[i].Cells[1].Value.ToString();
str2= dg.Rows[i].Cells[2].Value.ToString();

使用类似的方法读取值,然后传递到存储过程:

int i;
i = dg.SelectedCells[0].RowIndex;
str1= dg.Rows[i].Cells[1].Value.ToString();
str2= dg.Rows[i].Cells[2].Value.ToString();

您已经在循环所选行,因此不需要内部循环

只需使用选定的行

试试这个:

foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{    

        SqlCommand sqlCmd = new SqlCommand("uspSelectWater", con);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        sqlCmd.Parameters.AddWithValue("@CaseNumberKey", this._CaseNum);
        sqlCmd.Parameters.AddWithValue("@MasterAccount", r.Cells[3].Value);
        sqlCmd.Parameters.AddWithValue("@WaterAccount", r.Cells[4].Value);
        sqlCmd.Parameters.AddWithValue("@OwnerName", r.Cells[5].Value);
        sqlCmd.Parameters.AddWithValue("@MailName", r.Cells[6].Value);
        sqlCmd.Parameters.AddWithValue("@AcctBalance", r.Cells[7].Value);

        sqlCmd.ExecuteNonQuery();

}

您已经在循环所选行,因此不需要内部循环

只需使用选定的行

试试这个:

foreach (DataGridViewRow r in dataGridView1.SelectedRows)
{    

        SqlCommand sqlCmd = new SqlCommand("uspSelectWater", con);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        sqlCmd.Parameters.AddWithValue("@CaseNumberKey", this._CaseNum);
        sqlCmd.Parameters.AddWithValue("@MasterAccount", r.Cells[3].Value);
        sqlCmd.Parameters.AddWithValue("@WaterAccount", r.Cells[4].Value);
        sqlCmd.Parameters.AddWithValue("@OwnerName", r.Cells[5].Value);
        sqlCmd.Parameters.AddWithValue("@MailName", r.Cells[6].Value);
        sqlCmd.Parameters.AddWithValue("@AcctBalance", r.Cells[7].Value);

        sqlCmd.ExecuteNonQuery();

}

你不需要那个内环。一行有一个索引属性——您必须使用它。否则,它只是
r.Cells[3]。Value
,等等。您不需要那个内部循环。一行有一个索引属性——您必须使用它。否则,它只是
r.Cells[3]。Value
,等等。非常感谢。很好用!非常感谢。很好用!