Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 19-Aug-18 17:43pm

One of the options is to use the SortCompare event of DataGridView as shown below
VB
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
 
Share this answer
 
v2
Comments
7774tlas 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?
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.
 
Share this answer
 
Comments
7774tlas 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 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)
if the DataGridview is bound to a datasource, during add column ensure to include data type:

datatable.Columns.Add("Quantity", GetType(Integer))
 
Share this answer
 
Comments
Member 3126828 30-Aug-14 10:07am    
excelente respuesta, thanks a lot
Solution 4
I just changed list to add numeric item that was used to populate on grid. Then I was able to sort it as numeric field.

accountList.Add(new AccountList
                    {
                        BankAvailableBalance = (decimal)row["BankAvailableBalance"],
                        BankCurrentBalance = (decimal)row["BankCurrentBalance"]
                    });


On DataGrid I have following

<DataGrid Name="DataGridAccount" AutoGenerateColumns="False"
                  Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3">
   <DataGrid.Columns>
                <DataGridTextColumn Header="Bank Available Balance" Binding="{Binding BankAvailableBalance}"/>
                <DataGridTextColumn Header="Bank Current Balance" Binding="{Binding BankCurrentBalance}"/>

     </DataGrid.Columns>
</DataGrid>
 
Share this answer
 
Comments
CHill60 20-Aug-18 7:44am    
If you read the question carefully you will see that the OP posted "I have an app with an unbound DataGridView". You have demonstrated a solution using a bound DataGrid.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900