Click here to Skip to main content
11,479,412 members (51,519 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have an app with an unbound DataGridView and the first column's contents are millisecond values. The problem is that it sorts them as strings not integers.
Example:

21045
2305
340980
3700

when it should sort as:

2305
3700
21045
340980

I need it to sort the rows by the value of the first column[ms] from least(top) to greatest(bottom).

Can someone please help?
Posted 7-Apr-12 22:10pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

One of the options is to use the SortCompare event of DataGridView as shown below
Private Sub dataGridView1_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs)
    If e.Column.Index <> 0 Then
        Return
    End If
    Try
        e.SortResult = If(CInt(e.CellValue1) < CInt(e.CellValue2), -1, 1)
        e.Handled = True
    Catch
    End Try
End Sub
  Permalink  
v2
Comments
7774tlas at 18-Apr-12 22:21pm
   
Sorry for the late reply. Please forgive my ignorance, but most of this is still greek to me. I'm not sure how to implement this. My app plays an audio file, and when the "set" button is clicked, it adds the current position of the file in milliseconds along with other misc. info to the DataGridView programatically. I need the Sort list to update each time the "set" button is clicked and a new row of data is entered. I don't understand how or when I'm supposed to activate the Private Sub SortCompare. I understand that it compares the data in the cells, then sorts them, but what causes the SortCompare event?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I dont want to detract from VJ Reddy's solution, but if the DataGridview is not bound to a datasource, and are you adding the data programatically you can try writing an integer data type (or other numeric data type) to the DataGridview cells. Then it also sorts correctly.
  Permalink  
Comments
7774tlas at 18-Apr-12 21:46pm
   
Sorry for the late reply. Yes, the DataGridView is unbound, and I am adding the data programatically, so I believe your solution will work. Problem is, I'm a novice and have no idea how to set a data type to the cells in the first column.
Richard.Berry100 at 19-Apr-12 23:30pm
   
Lets say you are getting the data to put in the cell from a textbox called txtPrice.

Dim Price as Integer = Cint(Me.txtPrice.Text) 'Convert the value in the textbox to an integer

Me.Datagridveiw1.Rows(1).Cells("Price").Value = Price 'Put it in the datagrid

Basically you can use one of many covert functions BEFORE you put the value into the datagrid (Cint, CDbl, CBool, CStr)

You could even write it like this:

Me.Datagridveiw1.Rows(1).Cells("Price").Value = CInt(Me.txtPrice.Text)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if the DataGridview is bound to a datasource, during add column ensure to include data type:

datatable.Columns.Add("Quantity", GetType(Integer))
  Permalink  
Comments
Member 3126828 at 30-Aug-14 10:07am
   
excelente respuesta, thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 215
1 OriginalGriff 205
2 F-ES Sitecore 185
3 Frankie-C 120
4 DamithSL 85
0 Sergey Alexandrovich Kryukov 7,890
1 OriginalGriff 7,441
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 25 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100