• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

Sort DataGridView Columns

Hello EE,

I have a datagridview with like 10 columns..

3-4 columns starts with the word "Total"
some others with "Materials"
and the rest is other unique words...


what I would like is to sort the columns so that the user when he opens the form always that order:

Column with the word Total first... then all columns with "Materials" and after its not important

Right now I have some Materials between Total and its not good..


can you help ?
0
PhilippeRenaud
Asked:
PhilippeRenaud
1 Solution
 
CodeCruiserCommented:
0
 
nepaluzCommented:
Codewise, you can try
Dim xGrid As New DataGridView
Dim xOrder As Integer = 0
'first the columns containing total
Dim tCols = From x As DataGridViewColumn In xGrid.Columns Where x.Name.Contains("Total") Select x
For Each x In tCols
    x.DisplayIndex = xOrder
    xOrder = xOrder + 1
Next
'then the columns containing materials
Dim mCols = From x As DataGridViewColumn In xGrid.Columns Where x.Name.Contains("Materials") Select x
For Each x In mCols
    x.DisplayIndex = xOrder
    xOrder = xOrder + 1
Next
'then the rest
Dim rCols = From x As DataGridViewColumn In xGrid.Columns Where Not x.Name.Contains("Materials") OrElse Not x.Name.Contains("Total") Select x
For Each x In rCols
    x.DisplayIndex = xOrder
    xOrder = xOrder + 1
Next

Open in new window

0
 
PhilippeRenaudAuthor Commented:
excellent.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now