Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

C# DataSet is not updated after updating the DataGridView

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...