How to get array from column header With LINQ

Mansour_Dalir 1,876 Reputation points
2023-08-20T18:41:09.85+00:00

hi

20230820_220443

I want to get an array of the column header after changing the column display index

  dgv.AllowUserToOrderColumns = True
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,728 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 31,411 Reputation points Microsoft Vendor
    2023-08-21T03:24:05.9033333+00:00

    Hi @Mansour_Dalir ,

    You can refer to this link to the solution in C#, below is the code in the VB.net.

    Private Sub dgv_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgv.ColumnDisplayIndexChanged
            Dim g As DataGridView = DirectCast(sender, DataGridView)
            Dim propertyInfo As Reflection.PropertyInfo = GetType(DataGridViewColumn).GetProperty("DisplayIndexHasChanged", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance)
    
            If g.Columns.Cast(Of DataGridViewColumn)().Any(Function(x) DirectCast(propertyInfo.GetValue(x), Boolean)) Then
                Return
            Else
                Dim columnTitlesArray() As String = dgv.Columns.Cast(Of DataGridViewColumn)() _
                                               .OrderBy(Function(column) column.DisplayIndex) _
                                               .Select(Function(column) column.HeaderText) _
                                               .ToArray()
                For Each columnTitle As String In columnTitlesArray
                    Console.WriteLine(columnTitle)
                Next
            End If
    End Sub 
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Viorel 117.6K Reputation points
    2023-08-20T19:25:31.1133333+00:00

    In some circumstances, this should work:

    Dim columns As DataGridViewColumn() = dgv.Columns.OfType(Of DataGridViewColumn).OrderBy(Function(c) c.DisplayIndex).ToArray()
    
    0 comments No comments

  2. Jiachen Li-MSFT 31,411 Reputation points Microsoft Vendor
    2023-08-21T02:28:46.5166667+00:00

    Hi @Mansour_Dalir ,

    The following code gets an array of column headers every time the column order changes.

    Private Sub dgv_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgv.ColumnDisplayIndexChanged
                Dim columnTitlesArray() As String = dgv.Columns.Cast(Of DataGridViewColumn)() _
                                               .OrderBy(Function(column) column.DisplayIndex) _
                                               .Select(Function(column) column.HeaderText) _
                                               .ToArray()
    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 Answers by the question author, which helps users to know the answer solved the author's problem.