Hi, I have Form app that includes a DataGridView control (bound to DataTable). The user can sort the columns by clicking the column headers. If the user has clicked the Date header (to sort the column), the code below fails to update all rows in the DataGridView (and Database). The list is fairly large (few hundred), so I can only guess that when a certain number of rows are updated (via - row.Cells["Col_ItemAddedDate"].Value = newItemDate;) a resort occurs that mucks up the foreach loop? If I don't update the DGV cell, the loop works fine to update database. foreach (DataGridViewRow row in dataGridView1.SelectedRows) { OleDbCommand myCommand = new OleDbCommand("UPDATE Products SET ItemAddedDate=? WHERE ItemID=?", myConnection); myCommand.Parameters.Add("@ItemAddedDate", OleDbType.Date).Value = newItemDate; myCommand.Parameters.Add("@ItemID", OleDbType.BigInt).Value = row.Cells["ItemID"].Value; // execute the command myCommand.ExecuteNonQuery(); // THIS CODE CAUSES THE LOOP TO FAIL (if the Col_ItemAddedDate column header is clicked to sort) // Some of the rows are updated, but some do not (both Database UPDATE and the DGV cell update will fail) row.Cells["Col_ItemAddedDate"].Value = newItemDate; } Ron
modified on Monday, December 29, 2008 6:40 PM