[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Sort DataGridView Columns

Posted on 2012-04-05
Medium Priority
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 ?
Question by:PhilippeRenaud
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
LVL 83

Expert Comment

ID: 37813362
LVL 17

Accepted Solution

nepaluz earned 2000 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
'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
'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

Open in new window


Author Closing Comment

ID: 37823911

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 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