I am trying to update the data grid view (move a row to a new index). I can't update it directly because the data grid view is bound so I am doing this using the dataTable.
var dataTable = ((DataView)dataGridViewWebFields.DataSource).Table;
var dataGridRow = dataGridViewWebFields.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Cells[0].Value.Equals(fieldName));
if (dataGridRow != null && dataGridRow.Index != newIndex)
{
var dataTableRow = GetWebFieldFromDataGridViewRow(dataGridRow);
var dataViewRow = dataGridViewWebFields.Rows[newIndex];
dataViewRow.DefaultCellStyle = dataGridRow.DefaultCellStyle;
var newRow = dataTable.NewRow();
newRow.ItemArray = dataTableRow.ItemArray;
dataTable.Rows.Remove(dataTableRow);
dataTableRow.Index = newIndex;
dataTable.Rows.InsertAt(newRow, newIndex);
dataGridViewWebFields.DataSource = dataTable.AsDataView();
}
The problem is that after I am saving this to the database and refreshing the control, the state of the data grid view is back to the default (before the changes). I think that something is not updated correctly on the DataSet. Can you guide me on what am I doing wrong?
question from:
https://stackoverflow.com/questions/65830128/c-sharp-dataset-is-not-updated-after-updating-the-datagridview 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…