Prevent sorting DataGridView data while editing

I have a DatabaseGridView with a database binding in a Win Forms application that the user can sort by column. The problem is this: after the user leaves the row after editing the cell in the sorted column, the row will immediately be re-sorted.

This is very disorienting for users and makes it impossible to edit line groups.

The solution I'm looking for will effectively turn off automatic re-sorting after the initial sort, and then sort it again when the user requests it.

+4
source share
4 answers

In the interest of others, here is a solution that I came up with, but I would like to hear the best.

I added an extra, mutable column to the DataTable called SORT_ORDER, which is used only for sorting.

When the user clicks on the sorted column, I copy the values ​​and value type from the selected column to the SORT_ORDER column, and then sort by SORT_ORDER. Since SORT_ORDER is not displayed and cannot be changed, the sort order is not changed even if the user edits the selected column. The event handler is as follows:

private void MyDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { dirtyCellListenerEnabled = false; SORT_ORDER.ValueType = MyDataGridView.Columns[e.ColumnIndex].ValueType; foreach(DataGridViewRow r in MyDataGridView.Rows) { r.Cells[SORT_ORDER.Index].Value = r.Cells[e.ColumnIndex].Value; } switch(MyDataGridView.SortOrder) { case System.Windows.Forms.SortOrder.None: MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending); break; case System.Windows.Forms.SortOrder.Ascending: MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Descending); break; case System.Windows.Forms.SortOrder.Descending: MyDataGridView.Sort(SORT_ORDER, ListSortDirection.Ascending); break; } dirtyCellListenerEnabled = true; } 

Please note that I had to disconnect and re-enable my listener so that my code would not treat the update of the sort column as a real change.

Before proceeding with this solution, I also tried adding a sort column to the DataGridView, but it does not work because the DataGridView cannot sort a column that does not exist in its data source.

I'm sure there are other tweaks that I could do, for example, pause updates when filling out SORT_ORDER and display the sort glyph in the selected column.

+5
source

This is a real pain, as I now find out. Grids are sometimes cruelly complicated for seemingly nothing

for each selected cell, I store the primary key and the name of the grid column (I made a tiny class to hold them).

Then I drop them all into a list and iterate through them for updates. Each time I update the value of a cell, I look for where the actual cell is, and replace my local reference variable with that cell so that I can keep going in code.

  Cell.Value = ValueToWrite Cell = FindCell(Cell.OwningRow.DataGridView, DataRow, ColName) Function FindCell(Grid As DataGridView, DataRow As DataRow, ColName As String) As DataGridViewCell 'Find the same cell, wherever you may be now, damn you sort. Dim GridRow = (From x As DataGridViewRow In Grid.Rows Where x.DataBoundItem.row Is DataRow).FirstOrDefault Dim Cell = GridRow.Cells(ColName) Return Cell End Function 
+1
source

I ran into this problem and couldn't get a decent answer, so I tried this and it worked,

  private void SortBoundDG() { DataTable TempTable; TempTable = (DataTable)DG.DataSource; TempTable.DefaultView.Sort = ColumnName + " " + "DESC"; DG.DataSource = TempTable.DefaultView.ToTable(); } 

just convert the default view to a table and set it as the source for your datagridview

0
source

It looks like your GridView is binding data again. This means that your sort order will be lost. Turn on the ViewState of your gridview and make sure that you do not bind the grid to the postback.

-2
source

Source: https://habr.com/ru/post/1380489/


All Articles