Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand you correctly, you have already the grid filled with data and you want to change the content of a cell belonging to the column <code>Quantity</code> and referencing a row where the <code>ID</code> cell is used to find the updated value in the database.</p> <p>In this case you shouldn't rebind the grid again using the datatable, but just execute the command, retrieve the updated value and set the cell <code>Quantity</code> for the row with the ID requested</p> <pre><code>using (MySqlCommand cmd = new MySqlCommand(query, con)) { cmd.Parameters.AddWithValue("@ID", txtboxID.Text); object result = cmd.ExecuteScalar(); if(result != null) { int quantity = Convert.ToInt32(result); // Now you need to find the row that contains the ID passed DataGridViewRow row = grid.Rows .Cast&lt;DataGridViewRow&gt;() .Where(r =&gt; r.Cells["ID"].Value.ToString().Equals(txtBoxID.Text)) .First(); row.Cells["Quantity"].Value = quantity; } } </code></pre> <p><strong>UPDATE</strong><br> Following your comment, now it is clear that you have many records returned by the query, and you want to update many rows in the DataGridView.<br> This could be achieved with the following changes:</p> <pre><code>// The query returns also the Variant column from the database // The column is needed to identify the corresponding row to update on the datagridview // Also I am supposing that the variant column is from the same table (JOIN required otherwise) string query = @"SELECT variant, quantity FROM tblOrder_Products WHERE order_ID=@ID"; con.Open(); using (MySqlCommand cmd = new MySqlCommand(query, con)) { cmd.Parameters.AddWithValue("@ID", txtboxID.Text); // Cannot use a ExecuteScalar, we need a SqlDataReader to loop over the results SqlDataReader reader = cmd.ExecuteReader(); while(reader.Read()) { int quantity = reader.GetInt32(1); // Now I am supposing the the Variant column is of string type, change the Get // accordingly if it is not string v = reader.GetString(0); // Use the value retrieved from the database to identify the row to update DataGridViewRow row = grid.Rows .Cast&lt;DataGridViewRow&gt;() .Where(r =&gt; r.Cells["variant"].Value.ToString().Equals(v)) .First(); row.Cells["Quantity"].Value = quantity; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload