Click here to Skip to main content
14,599,199 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi How can i open a message box when i change my datagridview columns position

What I have tried:

<pre>Private Sub RAPOR_AllowUserToOrderColumnsChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RAPOR.AllowUserToOrderColumnsChanged
        MsgBox("JJJ")  END SUB
Posted
Updated 1-Mar-20 8:46am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try handling the DataGridView.ColumnDisplayIndexChanged Event (System.Windows.Forms) | Microsoft Docs[^] instead

ADDED
If I understand the situation correctly, you can use the event mentioned above and create a new data source each time the ordering changes.

For example if you have two DataGridView objects on a window (dgv1 and dgv2), consider the following code
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim data1 As DataTable = New DataTable()

        data1.Columns.Add("First")
        data1.Columns.Add("Second")
        data1.Columns.Add("Third")

        dgv1.DataSource = data1
        DisplayColumns()
    End Sub

    Private Sub dgv1_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgv1.ColumnDisplayIndexChanged
        DisplayColumns()
    End Sub

    Private Sub DisplayColumns()
        Dim data2 As DataTable = New DataTable()

        data2.Columns.Add("Ordinal", GetType(Integer))
        data2.Columns.Add("ColumnName", GetType(String))

        Dim colList = From column In dgv1.Columns.Cast(Of DataGridViewColumn) Order By column.DisplayIndex

        For Each col As DataGridViewColumn In colList
            data2.Rows.Add(col.DisplayIndex, col.HeaderText)
        Next

        dgv2.DataSource = data2
    End Sub
End Class
   
v2
Comments
Member 14588284 1-Mar-20 14:57pm
   
essentially i want this

when i changed datagrid column position then
another datagrid rows change like
my first datagrid columns header text
With FILTRE2
.Rows.Clear()
For s = 0 To RAPOR.Columns.Count - 1
.Rows.Add()
.Rows(.Rows.Count - 1).Cells(0).Value = .Rows.Count
.Rows(.Rows.Count - 1).Cells(1).Value = RAPOR.Columns(s).HeaderText
Next

End With
Wendelius 1-Mar-20 15:26pm
   
See the updated answer
Member 14588284 1-Mar-20 15:55pm
   
Thank you very much

i used this code
its working thans again

Private Sub RAPOR_ColumnDisplayIndexChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles RAPOR.ColumnDisplayIndexChanged
FILTRE2.Rows.Clear()
Dim colList = From column In RAPOR.Columns.Cast(Of DataGridViewColumn)() Order By column.DisplayIndex

For Each col As DataGridViewColumn In colList
FILTRE2.Rows.Add(col.DisplayIndex, col.HeaderText)
Next

End Sub
Wendelius 1-Mar-20 22:54pm
   
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100