I'm creating a DataGridView with move-record-up and move-record-down buttons. I've seen a lot written on this, but none of the solutions include a bound DataGridView. My problem is that I'm changing the DataGridView SortKey and only about 50% of the time does the data make it back to the database. The code I'm using is listed below.
If, in the same method, I write text to a textBox which is bound to the bindingSource, then the method works every time (indicating that the bound field is somehow forcing all the changed data back to the dataSource).
If someone is going to suggest doing the entire method at the dataSource level instead of in the dataGridView, I would appreciate some code snippet as I can't seem to figure out if you make changes to the DataTable where to force the resort and how to manage the current position.
private void buttonPhotoMoveUp_Click( object sender, EventArgs e )
int currentRowIndex = _dataGridViewPhoto.CurrentCell.RowIndex;
if ( currentRowIndex > 0 )
int currentRowSortKey = (int)( _dataGridViewPhoto.Rows[currentRowIndex].Cells["SortKey"].Value );
int previousRowSortKey = (int)( _dataGridViewPhoto.Rows[currentRowIndex - 1].Cells["SortKey"].Value );
// I swap the two keys here
_dataGridViewPhoto.Rows[currentRowIndex].Cells["SortKey"].Value = previousRowSortKey;
_dataGridViewPhoto.Rows[currentRowIndex - 1].Cells["SortKey"].Value = currentRowSortKey;
// This "commit" works only about 50% of the time. What am I missing to make
// the dataGridView push its changed data back to the datasource.
_sqlDataAdapterPhoto.Update( _dataSetMaster.Tables["Photo"] );
_dataGridViewPhoto.Sort( _dataGridViewPhoto.Columns["SortKey"], ListSortDirection.Ascending );
_dataGridViewPhoto.CurrentCell = _dataGridViewPhoto[0, currentRowIndex - 1];
// If I uncomment this, it works every time. The textBox is bound to the bindingSource.
//textBoxPhotoDetail.Text = textBoxPhotoDetail.Text + " ";