title: Updating DataGridView Selected Rows
body: <p>I tried to update selected rows in DataGridView, but the result is strange, it always missing a row or another. The problem is when I click btnSettled button to set the settled date, then click btnUpdate to update the database, the result seems ok, but after click btnRefresh to refresh the DGV, there is always a missing row. Is that the problem on UpdateCommand or foreach loop? Please help me to solve this problem. Thank you.</p>
  
  <p>before click btnSettle
  <a href="https://i.stack.imgur.com/26xwQ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/26xwQ.jpg" alt="before click btnSettled"></a></p>
  
  <p>after click btnSettled and btnUpdate
  <a href="https://i.stack.imgur.com/qJkB3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qJkB3.jpg" alt="after click btnSettled and btnUpdate"></a></p>
  
  <p>after click btnRefresh
  <a href="https://i.stack.imgur.com/s3jGx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s3jGx.jpg" alt="after click btnRefresh"></a></p>
  
  <p>My code as follows:</p>
  
  <pre><code>DataTable dtTrx = new DataTable();
  SqlDataAdapter daTrx = new SqlDataAdapter();
  DataSet dsTrx = new DataSet();
  
      public Form1()
      {
          InitializeComponent();
          getData();
      }
  
      private void getData()
      {
          string strConn = "Data Source=.\\xpw;Initial Catalog=MyStock;Integrated Security=True;";
          SqlConnection conn = new SqlConnection(strConn);
          conn.Open();
  
          string sqlTrx = "SELECT TrxID, TrxDate,Ticker,Qty,Price,Type,AccID, SettledDate,BrokerUserID FROM Trx";
  
          daTrx = new SqlDataAdapter(sqlTrx, conn);
          SqlCommandBuilder cbTrx = new SqlCommandBuilder(daTrx);
          daTrx.Fill(dsTrx, "trx");
  
          conn.Close();
  
          dtTrx = dsTrx.Tables["trx"];
          dgvTrx.DataSource = dtTrx;
      }
  
      private void btnUpdate_Click(object sender, EventArgs e)
      {
          daTrx.Update(dsTrx, "trx");
      }
  
      private void btnRefresh_Click(object sender, EventArgs e)
      {
          dsTrx.Clear();
          daTrx.Fill(dsTrx, "trx");
      }
  
      private void btnSettled_Click(object sender, EventArgs e)
      {
          foreach (DataGridViewCell c in dgvTrx.SelectedCells)
          {
              dgvTrx[7, c.RowIndex].Value = "2017/7/23";
          }
      }
  </code></pre>