Solved

Sort DataGridView Columns

Posted on 2012-04-05
3
454 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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