Results 1 to 3 of 3

Thread: Splitting a DataGrid Cell Value (Numeric) into individual numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    11

    Splitting a DataGrid Cell Value (Numeric) into individual numbers

    I am having dificulties taking a datagrid cell value stored in the following location (datagridVNTPSorted.Item(15), irow).Value) into individual digits to reassign values for counters.

    The cell in the datagrid (Listed Above) contains a number between 1 and 5624. I have 4 different counters. If the number was "2856", the counters would need to be updated as follows:

    intdirectoryCountDigit1 = 2
    intdirectoryCountDigit2 = 8
    intdirectoryCountDigit3 = 5
    intdirectoryCountDigit4 = 6

    If the number in datagridVNTPSorted.Item(15), irow).Value was = "24" then the values would be as follows:

    intdirectoryCountDigit1 = 0
    intdirectoryCountDigit2 = 0
    intdirectoryCountDigit3 = 2
    intdirectoryCountDigit4 = 4

    I attemped to use the Left and Right functions to return digits, but I keep on getting error messages. For Example: If the number of digits is 4, I attempted to get the correct values by doing the following:

    intDirectoryCountDigit1 = Int(Left(datagridVNTPSorted.Item(15, iRow), 1))

    intDirectoryCountDigit2 = Int(Right(Left(datagridVNTPSorted.Item(15, iRow), 2), 1))

    intDirectoryCountDigit3 = Int(Right(Left(datagridVNTPSorted.Item(15, iRow), 3), 1))

    intDirectoryCountDigit4 = Int(Right(datagridVNTPSorted.Item(15, iRow), 1))

    If the total number of digits was three, the first digit would always be 0

    If the total Number of Digits was 2, intDirectoryCountDigit 1 & 2 would always be 0

    and so on... but I get error messages.

    Any help is appreciated. Thanks

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Splitting a DataGrid Cell Value (Numeric) into individual numbers

    you may get errors because the data does not always have 4 digits, so the Right(x,y) does not always have a value and the left(x,y) may not be the one you want.

    Why dont you try to use the numeric value of the cell instead of the substring. That way you can do an integer divition. And use the Mod operator to eliminate the higher part of the cell value for the next divition.

    Code:
    intDirectoryCountDigit1 = datagridVNTPSorted.Item(15,iRow)\1000
    intDirectoryCountDigit2 = (datagridVNTPSorted.Item(15,iRow) Mod 1000)\100
    intDirectoryCountDigit3 = (datagridVNTPSorted.Item(15,iRow) Mod 100)\10
    intDirectoryCountDigit4 = (datagridVNTPSorted.Item(15,iRow) Mod 10
    Note that the division I used is with a backslash (\), this is to discard the decimals.
    Last edited by kaliman79912; Oct 17th, 2010 at 03:41 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

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

    Re: Splitting a DataGrid Cell Value (Numeric) into individual numbers

    Here's something a bit more general purpose:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim number = 2856
    
        For Each digit In Me.GetDigitArray(number, 4)
            MessageBox.Show(digit.ToString(), number.ToString())
        Next
    
        number = 24
    
        For Each digit In Me.GetDigitArray(number, 4)
            MessageBox.Show(digit.ToString(), number.ToString())
        Next
    End Sub
    
    Private Function GetDigitArray(ByVal number As Integer, ByVal count As Integer) As Integer()
        Dim text As String = number.ToString().PadLeft(count, "0"c)
        Dim characters As Char() = text.ToCharArray()
        Dim digits As Integer() = Array.ConvertAll(characters, Function(ch) Convert.ToInt32(ch, 10))
    
        Return digits
    End Function
    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

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