Solved

Sort DataGridView Columns

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now