Results 1 to 3 of 3

Thread: Sorting DataGridView depending on String or Numeric content

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Question Sorting DataGridView depending on String or Numeric content

    Firstly, I am using Visual Studio 2013

    Secondly, I am converting what to me is a very large project from vb6 so changing the way it is organised is not an option - it is hard enough making a direct conversion actually work (9 months so far)

    So, I have a form with 4 DataGridViews on it, each with up to 10 columns. The contents of each column can vary depending on lots of factors so it may be strings or it may be numeric data stored as strings. To change this so numeric data is actually held as numeric data will mean making dozens of changes each with the likelihood of screwing it up

    So, when a user clicks on the header I want to intercept the sort routine to check if the column is composed of numeric or string data. This is so that if it is numeric it sorts as:

    7
    8
    9
    77

    and not as

    7
    77
    8
    9

    Worst case, I suppose I could do something like:

    Code:
    Private Sub GridsAll_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles _ 
      Grid1.ColumnHeaderMouseClick,
      Grid2.ColumnHeaderMouseClick,
      Grid3.ColumnHeaderMouseClick,
      Grid4.ColumnHeaderMouseClick
    
      ' Special sort goes here ...
    
    End Sub
    But maybe there is an easy way - all suggestions welcomed!

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Sorting DataGridView depending on String or Numeric content

    If the datatype for the filed is a number then it should function this way automatically ... if not you can use the StrCmpLogicalW API to compare strings with numbers in it to do what you want:

    vb Code:
    1. Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" (ByVal s1 As String, ByVal s2 As String) As Int32

    You will also need to use this in conjunction with an IComparer

    Kris

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2011
    Posts
    87

    Re: Sorting DataGridView depending on String or Numeric content

    Well, I have just found a very simple way of doing this for which I claim no credit!

    But first, it seems that the default mode for a DataGridView cell is "TextBox" (as you will notice if you add a column) and surprisingly if you do the following (as I have done), it allows it

    Code:
    Grid1.Item(5, intRow).Value = 17.356
    Even if you have Option Strict on (which you should IMHO) but it stores it as a String (news to me but I should have realised)

    Anyway, if you add the following routine, it should work like magic

    Code:
    Private Sub Grid_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs) Handles _
      Grid1.SortCompare,
      Grid2.SortCompare,
      Grid3.SortCompare,
      Grid4.SortCompare
    
      Dim MyGrid As DataGridView
    
      MyGrid = DirectCast(sender, DataGridView)
    
      If GridColumnNumericLGH(MyGrid, e.Column.Index) = False Then
          Return
        Else
          e.SortResult = If(Cint(e.CellValue1) < CInt(e.CellValue2), -1, 1)
          e.Handled = True
      End If
    
    End Sub
    
    Public Function GridColumnNumericLGH(MyGrid As DataGridView, intColumn As Integer) As Boolean
      ' Written 7th November 2014
    
      Dim intRow As Integer
    
      GridColumnNumericLGH = True
      For intRow = 0 to MyGrid.RowCount - 1
        If IsNumeric(MyGrid.Item(intColumn, intRow).Value) = False Then
          GridColumnNumericLGH = False
          Exit Function
        End If
      Next
    
    End Function
    I hope this heps somebody
    Last edited by wavering; Nov 7th, 2014 at 07:31 AM.

Tags for this Thread

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