Link to home
Create AccountLog in
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
Avatar of Paulo Pimenta
Paulo Pimenta
Flag of Portugal image

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
Avatar of giviglie
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...

I can't seem to be able to replicate that situation.
It work fine with me.
(See picture attrached)
datagridview.jpg
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
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
Avatar of Paulo Pimenta
Paulo Pimenta
Flag of Portugal image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account