This is a migrated thread and some comments may be shown as answers.

Avoid sorting when adding row

1 Answer 192 Views
GridView
This is a migrated thread and some comments may be shown as answers.
OD
Top achievements
Rank 1
OD asked on 18 Jun 2018, 12:52 PM

Hi Telerik team :)

I have a gridview with a list of persons.

I sort this list on the name of persons when i click on column header.

Now, when i click on a row to add a new person, when i end-edit the row, the new person is automaticaly sorted with the rest of the list.

I don't want this behavior.

I'll prefer that the new person stays in bottom of list with the list already sorted.

So is there a way to avoid the sort when add a row without modifying the current list display (even if sorted) ?

Thanks

Regards 

 

 

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 19 Jun 2018, 10:23 AM
Hello, Jean-Marc,  

Let me please explain in details how the editing mechanism in RadGridView works. By default, no editor is available in the grid. If you double click a cell or press F2 when a certain cell is selected, RadGridView activates the relevant editor for that cell and RadGridView enters edit mode. After you enter the desired value in the editor you can commit the value by pressing the Enter key or navigate to another cell. Pressing the Escape key will cancel the edit operation. Note that once the editor's value is committed, the underlying DataBoundItem is updated accordingly. This is the designed behavior. Have in mind that once the grid is sorted by a certain column, it orders the rows considering the applied SortDescriptor and the cells' values belonging to this column. The SortDescriptor is responsible for keeping the available rows sorted according to the applied criteria. That is why when you add a new row or edit an existing one, they are placed at the correct index according to the applied sorting.

A possible solution is to use the custom sorting functionality where you can fully manage how the rows are ordered:  https://docs.telerik.com/devtools/winforms/gridview/sorting/custom-sorting. Alternatively, RadGridView allows you to prevent the built-in data sorting operation but keep the sorting life cycle as it is, e.g. UI indication, SortDescriptors and events remain. This is controlled by the MasterTemplate.DataView.BypassSort property which default value is false. This means that RadGridView won't perform the sorting if you set it to true. This may be suitable for cases in which you bound the grid to a DataTable and you want to apply the sort direction to the DataTable, not to the grid itself. A sample code snippet is available in the following help article: https://docs.telerik.com/devtools/winforms/gridview/sorting/basic-sorting 

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Jay
Top achievements
Rank 1
Iron
Iron
commented on 13 Jun 2023, 08:31 PM

Neither article indicates which of the many sort algorithms prevents a newly inserted row from being sorted. Nor is it clear why this particular problem of preventing a newly-inserted row from being sorted isn't solved with a boolean property. It strikes me clearly as a design flaw.
Dess | Tech Support Engineer, Principal
Telerik team
commented on 14 Jun 2023, 04:41 AM

Hi, Jay,

Please have in mind that the sorting functionality is not a one-time operation in RadGridView. When you add a SortDescriptor for a certain column, it is responsible for the correct order of the rows considering the cell values in this column.

I believe that the following KB article will bring some useful details about the sorting behavior in RadGridView:

https://docs.telerik.com/devtools/winforms/knowledge-base/grid-cells-in-sorted-columns-jumps-to-a-new-position-after-editing-its-value 

The possible approaches which I can suggest in this situation if you want to control which rows are sorted is either use the custom sorting functionality which was previously suggested or consider using RadVirtualGrid.

Jay
Top achievements
Rank 1
Iron
Iron
commented on 15 Jun 2023, 05:33 AM

Dess,

I simply want to prevent newly added rows from automatically disappearing from the client area of the gridview. I think that's a reasonable UI principle, don't you? I can't release my software with RadGridView this way because my users will kill me with one simple question: "Where did my data go?"

That's why I believe it's a design flaw of RadGridView to not provide a mechanism to prevent the live sorting of a newly added row. It's perfectly fine to leave the new row at the top of the grid. If the user wants to sort, then they can click on the column headers.

If developers need a sorted list such as for data validation, they can sort the list when the user is done with the grid.

I tried implementing a custom sort and it failed to prevent the auto-sort of newly added rows.

Also, and perhaps critically with regard to the article you sent and which I read, I'm not using any sort descriptors, so there shouldn't be any reason for the gridview to sort the newly added row.

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 19 Jun 2023, 08:33 AM

Jay, Thank you for the provided detailed information about the scenario you have. Note that when the use clicks a column header, this column is sorted via adding a SortDescriptor internally. This is how the grid sorts its rows.

The possible solution that I can suggest is to ensure that the newly added row is visible in the current view. Please set the MasterTemplate.SelectLastAddedRow property to true. When the new row is added via the UI, the UserAddedRow event will be fired. Thus, you can make the new row CurrentRow for the grid: 

        public RadForm1()
        {
            InitializeComponent();

            this.radGridView1.Columns.Add("Id");
            this.radGridView1.Columns.Add("Title");
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            for (int i = 0; i < 20; i++)
            {
                this.radGridView1.Rows.Add(i, Guid.NewGuid().ToString());
            }

            this.radGridView1.MasterTemplate.SelectLastAddedRow = true;
            this.radGridView1.UserAddedRow+=radGridView1_UserAddedRow;
        }

        private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            this.radGridView1.CurrentRow = e.Row;
        }

Please give the sample approach a try and see how it works on your end. I believe that it would be applicable for your scenario.

Tags
GridView
Asked by
OD
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or