Avatar of giviglie
giviglie
 asked on

Sorting a Dates and Number on a Datagidview

That's my question...
I come from vb6...
I have no time for now to learn how to use ADO.NET . I'm quite good with ADO...

I can populate a datagridview looping through a recordset.

My problem now is how to sort columns with dates and numbers....

First solution is to repopulate every time the datagridview with a SQL instructions..
That could be possible because records are not too much (100-200 rows)

Second and that's what I'm trying to do is to sort without calling a SQL instruction...

But I cant'really understand how the datagridview manage sorting..

Any help,,,,will be very appreciated...


Thanx

Giacomo
.NET ProgrammingMySQL Server

Avatar of undefined
Last Comment
Paulo Pimenta

8/22/2022 - Mon
Paulo Pimenta

I don't know if you're talking about ASP.NET gridviews. These allow sorting in a real easy way.
No need to "re-query" the data. ASP.NET does that for you.
(See picture attached)
gridview-sorting.jpg
giviglie

ASKER
thnx for your answer...
no i'm not talking about asp...

The problem in datagridview sorting is that all is sorted like a string...

Paulo Pimenta

I can't seem to be able to replicate that situation.
It work fine with me.
(See picture attrached)
datagridview.jpg
Your help has saved me hundreds of hours of internet surfing.
fblack61
giviglie

ASKER
probably you have the datagridview directly connected to a dataset. In that case the Datagridview can understand what kind of type of data had to sort.
Not in my case...

Giacomo
giviglie

ASKER
Dear Paulop75,
thanks for your answer.
I've found the solution. I post my code here because someone else maybe may have the same problem I had.

MY SOLUTION

I've decided to built the grid runtime. So I can set all the properties I need.
Here you have a little example of my code.


        Dim colonnadata As New DataGridViewTextBoxColumn
        Dim colonnanumero As New DataGridViewTextBoxColumn
       
        colonnanumero.ValueType = GetType(Decimal)
        colonnadata.ValueType = GetType(Date)


        colonnadata.Width = 100
        colonnadata.HeaderText = "MY DATE"
        Form1.DataGridView1.Columns.Add(colonnadata)

        colonnanumero.Width = 100
        colonnanumero.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        colonnanumero.DefaultCellStyle.Format = "C2"
        colonnanumero.HeaderText = "PRICE"
        Form1.DataGridView1.Columns.Add(colonnanumero)

Now in sorting everything is ok....

Bye

Giacomo

ASKER CERTIFIED SOLUTION
Paulo Pimenta

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.