Solved

Sort DataGridView Columns

Posted on 2012-04-05
3
466 Views
Last Modified: 2012-04-09
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
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37813362
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 37816678
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
 
LVL 1

Author Closing Comment

by:PhilippeRenaud
ID: 37823911
excellent.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question