PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Get next Column In DisplayOrder from DGV-VBForums
Results 1 to 4 of 4

Thread: Get next Column In DisplayOrder from DGV

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Get next Column In DisplayOrder from DGV

    Hello.

    I have set the Tags of DGVColumns as True or False. Based on some settings the DisplayOrder of the Columns are changed at runtime.

    I need to get the ColumnIndex of the next column from current column which has Tag as True

    Plz Help
    Thanks

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,912

    Re: Get next Column In DisplayOrder from DGV

    It would help if you used the actual terminology rather than stuff that you made up yourself that is vaguely similar. DisplayOrder and ColumnIndex mean nothing in this context. I think that what you're actually saying is that you want the Index of the column that has the next highest DisplayIndex and a Tag of True.

    My first question is whether you actually want the Index of the next column or the column itself. The reason is that you would normally only be interested in the index to get the column but, in this case, you need to use the column to get the index so the last step is pointless if the column is what you actually want. Anyway, here's how I would do it:
    vb.net Code:
    1. Dim nextColumn = myDataGridView.Columns.
    2.                                 Cast(Of DataGridViewColumn)().
    3.                                 Where(Function(dgvc) CBool(dgvc.Tag)).
    4.                                 OrderBy(Function(dgvc) dgvc.DisplayIndex).
    5.                                 FirstOrDefault(Function(dgvc) dgvc.DisplayIndex > currentColumn.DisplayIndex)
    6.  
    7. If nextColumn IsNot Nothing Then
    8.     Dim index = nextColumn.Index
    9.  
    10.     '...
    11. End If

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Get next Column In DisplayOrder from DGV

    Thanks for the Help..
    Apologies for the Terminology.

    I wanted only the column index.

    Can u plz help with a solution without LINQ, I am not familiar with it.

    Regards

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,145

    Re: Get next Column In DisplayOrder from DGV

    Here is a non-Linq version (based on the Linq):
    Code:
    Dim nextColumnIndex as DataGridViewColumn = Nothing
      For Each possibleColumn In myDataGridView.Columns
         If CBool(possibleColumn.Tag) AndAlso possibleColumn.DisplayIndex > currentColumn.DisplayIndex Then
            If nextColumn is Nothing OrElse possibleColumn.DisplayIndex < nextColumn.DisplayIndex
               nextColumn = possibleColumn
            End if
         End If
      Next 
    
      If nextColumn IsNot Nothing Then
        Dim index = nextColumn.Index
    ...
    This is just typed directly into the browser, so there may be minor mistakes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width