Results 1 to 5 of 5

Thread: [RESOLVED] Convert Celltype

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: Convert Celltype

    You need to do the custom sort:

    vb.net Code:
    1. Private Sub DataGridView1_SortCompare( _
    2.     ByVal sender As Object, _
    3.     ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs _
    4. ) Handles DataGridView1.SortCompare
    5.  
    6.     Dim int1, int2 As Integer
    7.  
    8.     If Not Integer.TryParse(e.CellValue1.ToString, int1) Then Return
    9.     If Not Integer.TryParse(e.CellValue2.ToString, int2) Then Return
    10.  
    11.     If int1 = int2 Then
    12.         e.SortResult = 0
    13.     ElseIf int2 > int1 Then
    14.         e.SortResult = -1
    15.     Else
    16.         e.SortResult = 1
    17.     End If
    18.  
    19.     e.Handled = True
    20. End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Convert Celltype

    Quote Originally Posted by Deepak Sakpal
    You need to do the custom sort:

    vb.net Code:
    1. Private Sub DataGridView1_SortCompare( _
    2.     ByVal sender As Object, _
    3.     ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs _
    4. ) Handles DataGridView1.SortCompare
    5.  
    6.     Dim int1, int2 As Integer
    7.  
    8.     If Not Integer.TryParse(e.CellValue1.ToString, int1) Then Return
    9.     If Not Integer.TryParse(e.CellValue2.ToString, int2) Then Return
    10.  
    11.     If int1 = int2 Then
    12.         e.SortResult = 0
    13.     ElseIf int2 > int1 Then
    14.         e.SortResult = -1
    15.     Else
    16.         e.SortResult = 1
    17.     End If
    18.  
    19.     e.Handled = True
    20. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    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
  •  



Click Here to Expand Forum to Full Width