title: Why foreach loop does not work with datagridview selected rows in C#?
body: <p>I am new to c# and using windows forms.</p>
  
  <p>The following code loops through all selected rows in <code>datagridview</code> and take variables from cell0 and 4 as condition to use in sql query.</p>
  
  <p>I multi-select rows then use this code and when I check the result in sql table I find this code only consider the first selected row in datagridview and ignore the rest.</p>
  
  <p>Anyone knows how can I fix it, or what I am doing wrong? please help , thank you</p>
  
  <pre><code> foreach (DataGridViewRow row in DGV.SelectedRows)
                  {
  
  
  
                  OrderNumber = Convert.ToInt32(DGV.SelectedRows[0].Cells[0].Value);
                  OrderDateTime = Convert.ToDateTime(DGV.SelectedRows[0].Cells[4].Value);
  
                  MyConnection.Open();
                  MyCommand.CommandText = "UPDATE List_of_All_Orders set Delivery_State=@_Delivery_State WHERE Order_Number=@_OrderNumber and Date_Time_Ordered =@_OrderDateTime";
  
                  MyCommand.Connection = MyConnection;
  
                  MyCommand.Parameters.Add("@_Delivery_State", SqlDbType.NVarChar).Value = button7.Text;
                  MyCommand.Parameters.Add("@_OrderNumber", SqlDbType.Int).Value = OrderNumber;
                  MyCommand.Parameters.Add("@_OrderDateTime", SqlDbType.DateTime).Value = OrderDateTime;
  
                  MyCommand.ExecuteNonQuery();
  
                  MyCommand.Parameters.Clear();
                  MyConnection.Close();
  
  
  
                  }
  </code></pre>