VB 2015 : How To prevent Column Ordering between Frozen Columns in DataGridView ?

Marco Gregori 41 Reputation points
2023-03-23T13:49:18.42+00:00

Hi,

I have a DataGridView Control with a bunch of Columns and Rows.
My task is to have the first 3 Columns Frozen and Fixed in order.
User can't change order on Columns from Column01 to Column03.
User can change order only on Columns from Column04 to Column09.

Here's an example :


        For i As Integer = 1 To 9
            DGV.Columns.Add("Column" & i.ToString.PadLeft(2, "0"c),
                            "Column" & i.ToString.PadLeft(2, "0"c))
        Next
        Dim values As List(Of Object)
        For i As Integer = 1 To 100
            values = New List(Of Object)
            For j As Integer = 1 To 9
                values.Add("Data" & i.ToString.PadLeft(5, "0"c) & "-" & j.ToString)
            Next
            DGV.Rows.Add(values.ToArray)
        Next
        DGV.AllowUserToOrderColumns = True
        DGV.Columns(0).Frozen = True
        DGV.Columns(1).Frozen = True
        DGV.Columns(2).Frozen = True

The problem is that User can still change order between the 3 first Frozen Columns.
What should be done to prevent User to move the Forzen Columns between them ?

Thanks to anyone able to help.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,401 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 26,671 Reputation points Microsoft Vendor
    2023-03-24T02:53:13.6233333+00:00

    Hi, welcome to Microsoft Q&A. You can refer to the following code.

        Private Sub DGV_MouseUp(sender As Object, e As MouseEventArgs) Handles DGV.MouseUp
            If DGV.Columns(0).DisplayIndex <> 0 OrElse DGV.Columns(1).DisplayIndex <> 1 OrElse DGV.Columns(2).DisplayIndex <> 2 Then
                DGV.Columns(0).DisplayIndex = 0
                DGV.Columns(1).DisplayIndex = 1
                DGV.Columns(2).DisplayIndex = 2
            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.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sedat SALMAN 13,165 Reputation points
    2023-03-23T20:29:17.28+00:00

    You can handle the DataGridView.ColumnDisplayIndexChanged event to prevent the user from changing the order of the first three frozen columns. In the event handler, check if the new display index of any of the first three columns is different from their initial positions. If it's different, set it back to the original position.

    Here's the updated code:

    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i As Integer = 1 To 9
                DGV.Columns.Add("Column" & i.ToString.PadLeft(2, "0"c),
                                "Column" & i.ToString.PadLeft(2, "0"c))
            Next
            Dim values As List(Of Object)
            For i As Integer = 1 To 100
                values = New List(Of Object)
                For j As Integer = 1 To 9
                    values.Add("Data" & i.ToString.PadLeft(5, "0"c) & "-" & j.ToString)
                Next
                DGV.Rows.Add(values.ToArray)
            Next
            DGV.AllowUserToOrderColumns = True
            DGV.Columns(0).Frozen = True
            DGV.Columns(1).Frozen = True
            DGV.Columns(2).Frozen = True
    
            ' Handle the ColumnDisplayIndexChanged event
            AddHandler DGV.ColumnDisplayIndexChanged, AddressOf DGV_ColumnDisplayIndexChanged
        End Sub
    
        Private Sub DGV_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs)
            ' Check if any of the first three columns changed their display index
            If e.Column.Index < 3 AndAlso e.Column.DisplayIndex <> e.Column.Index Then
                ' Set the display index back to the original position
                e.Column.DisplayIndex = e.Column.Index
            End If
        End Sub
    End Class
    
    

    This code will prevent the user from changing the order of the first three frozen columns. They can still change the order of the remaining columns as needed.