|
-
Feb 16th, 2009, 12:32 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Convert Celltype
Hi, i have a column in datagridview composed of numeric as well as aphanumeric. What i want is when the user sorts the column, it will be sorted according to its number value, 1,2,3,4,5,6,7,8,9,10. However it my current module, the program sorts basing the first digit of the column. like 1,10,11,12,2,3,30,31.
i was thinking of converting the type to number so that it will be arrange properly, is this possible in the datagridview to covert type? because i saw only a readonly property celltype?
i have also tried casting it in the sql query itself to INT but it did not work.
do you have any suggestion for my case? thank you. i will appreciate your help.
-
Feb 16th, 2009, 12:46 AM
#2
Re: Convert Celltype
The DataGridView will sort the values in a column based on the type of those values. If the cells contain text then the column will be sorted "alphabetically". If the cells contain numbers then the column will be sorted numerically. Text that contains digit characters is still text, not a number.
There's nothing you can do to the column or the cells that is going to change that behaviour. All that matters is what data you assign, either explicitly or implicitly (e.g. by setting the grid's DataSource), to the Value properties of the cells.
If you want to override this default behaviour then you have to write code to sort the column yourself, instead of letting the grid do it for you. You should read the documentation for the DataGridView.Sort method. It provides an explanation and an example.
-
Feb 16th, 2009, 12:55 AM
#3
Re: Convert Celltype
You need to do the custom sort:
vb.net Code:
Private Sub DataGridView1_SortCompare( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs _ ) Handles DataGridView1.SortCompare Dim int1, int2 As Integer If Not Integer.TryParse(e.CellValue1.ToString, int1) Then Return If Not Integer.TryParse(e.CellValue2.ToString, int2) Then Return If int1 = int2 Then e.SortResult = 0 ElseIf int2 > int1 Then e.SortResult = -1 Else e.SortResult = 1 End If e.Handled = True End Sub
-
Feb 16th, 2009, 01:05 AM
#4
Re: Convert Celltype
 Originally Posted by Deepak Sakpal
You need to do the custom sort:
vb.net Code:
Private Sub DataGridView1_SortCompare( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs _ ) Handles DataGridView1.SortCompare Dim int1, int2 As Integer If Not Integer.TryParse(e.CellValue1.ToString, int1) Then Return If Not Integer.TryParse(e.CellValue2.ToString, int2) Then Return If int1 = int2 Then e.SortResult = 0 ElseIf int2 > int1 Then e.SortResult = -1 Else e.SortResult = 1 End If e.Handled = True End Sub
The documentation for that event says:
This event occurs only when the DataSource property is not set
As the grid in this case is being populated with the result of a SQL query I'm guessing that the grid is being bound, thus precluding this as an option. It would be the way to go for an unbound grid though.
-
Feb 16th, 2009, 01:13 AM
#5
Thread Starter
Addicted Member
Re: Convert Celltype
thank you so much! yes, my grid is bounded to a datasource, as you have suggested i think i will try to sort it programmatically instead of letting the grid sort the column. Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|