question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to disable row shifting when column is sorting in datatable

See original GitHub issue

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 . (vb.net-vs2019) This is a weakness in ‘DataTable’ or ‘DataGridView’? the biggest problem. Will this problem be solved in the future? In other versions of DataTable or DataGridView. It would be great if it had an option named (orderFix). How can this feedback be reported? 20230609_155121 (1)

 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
'There is no difference between the two (Programmatic /NotSortable)
    dgv.Columns(0).SortMode = DataGridViewColumnSortMode.Programmatic' /NotSortable
    dgv.Columns(1).SortMode = DataGridViewColumnSortMode.Programmatic '/NotSortable
    dgv.Columns(2).SortMode = DataGridViewColumnSortMode.Programmatic' /NotSortable
    dgv.Columns(3).SortMode = DataGridViewColumnSortMode.Programmatic' /NotSortable
End Sub

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    MyDataTable.DefaultView.Sort = "[f2] asc"
End Sub

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
John-Qiaocommented, Jun 21, 2023

@idmansourid I didn’t find a default property or method to achieve that in DGV control, but maybe you can try below workaround to sort only column f2 in an array and re-assign values to column f2.

    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 Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'MyDataTable.DefaultView.Sort = "[f2] asc"

        Dim array1 As String() = New String(MyDataTable.Rows.Count - 1) {}

        For i As Integer = 0 To MyDataTable.Rows.Count - 1
            array1(i) = MyDataTable.Rows(i).Item(2).ToString()
        Next

        Array.Sort(array1)

        For i As Integer = 0 To MyDataTable.Rows.Count - 1
            MyDataTable.Rows(i).Item(2) = array1(i)
        Next

    End Sub
1reaction
idmansouridcommented, Jun 16, 2023

I posted this question in (StackOverflow) but unfortunately it was closed due to lack of attention and (downvoting). And now I am blocked from posting questions in (StackOverflow).I couldn’t find a way to get out of being blocked (StackOverflow). I’m still blocked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to disable row shifting when column is sorting in ...
When it is sorted, when I give a value to the cell, the row moves. I don't want this to happen. I work...
Read more >
How to disable row shifting when column is sorting in ...
When it is sorted, when I give a value to the cell, the row moves. I don't want this to happen. I work...
Read more >
disable sorting on specific row using css class
i have only one problem in my implementation: i have a grid with column that i don't want to be sortable (there are...
Read more >
How to disable sorting in Table visual
1. Create a blank button or blank rectangle · 2. Place it over the Table columns (Make sure you place the blank button/rectangle...
Read more >
Data Grid - Sorting
Easily sort your rows based on one or several criteria. ... Hold down the Ctrl or Shift (use ⌘ Command on macOS) key...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found