How to disable row shifting when column is sorting in datatable (waiting for an answer)

Mansour_Dalir 2,036 Reputation points
2023-06-09T12:31:32.0166667+00:00

hi

When it is sorted, when I give a value to the cell, the row moves. I don't want this to happen. I work with DataGridView and DataTable .

20230609_155121

    Dim MyDataTable As New DataTable
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MyDataTable.Columns.Add("id")
        MyDataTable.Columns.Add("f1")
        MyDataTable.Columns.Add("f2")
        MyDataTable.Columns.Add("f3")
    MyDataTable.Rows.Add({"1", "a1", "b1", "c1"})
    MyDataTable.Rows.Add({"2", "a2", "b2", "c2"})
    MyDataTable.Rows.Add({"3", "a3", "b3", "c3"})
    dgv.DataSource = MyDataTable
    dgv.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic
    dgv.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic
    dgv.Columns(2).SortMode = DataGridViewColumnSortMode.Programmatic
    dgv.Columns(3).SortMode = DataGridViewColumnSortMode.Programmatic
End Sub

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    MyDataTable.DefaultView.Sort = "[f2] asc"
End Sub
Developer technologies | VB
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2023-06-19T06:29:06.8033333+00:00

    Hi @Mansour_Dalir ,

    When you turn on sorting, exiting edit mode triggers automatic sorting, which is the expected result.

    If you want it to sort automatically and not trigger sorting after you modify the cell contents, then you may need to disable sorting in DataGridView.CellEndEdit Event and then use your button to re-enable sorting.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2023-06-23T06:56:13.6866667+00:00

    Hi @Mansour_Dalir ,

    You can try the following code to use another datatable to store the sorted data and make the dgv display it.

        Private SortedDataTable As DataTable
    
        Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
            Dim sortedView As DataView = MyDataTable.DefaultView
            sortedView.Sort = "[f2] asc"
            SortedDataTable = sortedView.ToTable()
    
            dgv.DataSource = SortedDataTable
        End Sub
    
        Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
            If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
                Dim modifiedRow As Integer = e.RowIndex
                Dim modifiedColumn As Integer = e.ColumnIndex
    
                Dim modifiedValue As Object = dgv.Rows(modifiedRow).Cells(modifiedColumn).Value
    
                Dim id As Integer = Convert.ToInt32(dgv.Rows(modifiedRow).Cells("id").Value)
    
                Dim rows As DataRow() = MyDataTable.Select("id = " & id)
                If rows.Length > 0 Then
                    rows(0)(modifiedColumn) = modifiedValue
                End If
            End If
        End Sub
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.