Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort a datagrid on form load? 2

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
I have a form with a datagrid on it. The table only has two columns. I'm only showing one of those columns. The column I am showing is called "Categories". The hidden field is the primary key and the data is being sorted on that by default.

When the form opens I can click the header in the datagrid and get the data to sort by the Category. Is there a way to sort on this column at form load?

All of my searches have resulted in solutions that involved adding of additional columns which just doesn't seem right to me. If I can click it, there should be a way to mimic that event in code right?

Any assistance is greatly appreciated.
 
You didn't specify how you are populating the datagrid, normally when I want to sort a datagrid I do the sorting with the query that I populate the datagrid with. Other than that you can use the bindingsource.sort to sort data the way you want it.
 
Another method:

Code:
        Dim dv As New DataView
        dv.Table = YourDataTable
        dv.Sort = "Category"
        YourDataGridView.DataSource = dv
 
Thank you both. I ended up using kliot's suggestion since it was the easiest to implement, however it is great to know how to do it programatically as well.

My user interface if for the most part done. Now I need to move on to more difficult tasks. Will start a new thread after I do some searching to try to help myself before asking for help.

Thanks again guys. Your help has been much appreciated and is helping me to discover parts of Visual Basic I didn't know existed.
 
Glad to help, I remember how much fun it was when I started learning vb.net, not. Post if you get stuck again.

Perrin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top