Results 1 to 27 of 27

Thread: [RESOLVED] [2005] Total count of string in a DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Resolved [RESOLVED] [2005] Total count of string in a DataGridView

    How can i get a total count of a specific string from an entire column in a datgridview?


    Thanks.
    2005

  2. #2
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Total count of string in a DataGridView

    Well, you can loop through each cell in the column and search for the string. Every time it is found you can increase a counter variable.
    Prefix has no suffix, but suffix has a prefix.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Troy, that sounds very nice, unfortunately i'm just a beginner. Can you provide some code? Thanks.

    I use this code to calculate totals in a datagridview buti don'tknow how to mod it to count a specific string, not numbers.

    Code:
            If Me.ProjectsBindingSource.Position > -1 Then
                Dim totalQA As Double = 0
                For Each row As DataRowView In Me.ProjectDescriptionsBindingSource.List
                    totalQA += row!QATotalCompletion
                Next
                Me.QATotalCountCompletionTextBox.Text = Format(totalQA)
            Else
                Me.QATotalCountCompletionTextBox.Text = 0
            End If
    Any help will be appreciated!
    2005

  4. #4
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Re: [2005] Total count of string in a DataGridView

    Say the column you want to search is the first column.

    vb.net Code:
    1. Dim ColumnToSearch As Integer = 0
    2. Dim StringToFind As String = "Hello"
    3. Dim cnt As Integer = 0

    Now loop through each row and grab the text from the cell.
    vb.net Code:
    1. Dim s As String
    2. For Each row As DataGridViewRow in myDataGridView.Rows
    3. s = row.Cells(ColumnToSearch)
    4. If s.Contains(StringToFind) Then
    5. cnt += 1
    6. Next

    cnt will contain the number of instances found.
    Prefix has no suffix, but suffix has a prefix.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    I'm getting this error:

    Error 1 Value of type 'System.Windows.Forms.DataGridViewCell' cannot be converted to 'String'.

    It seems to be here:
    Code:
    s = row.Cells(ColumnToSearch)
    From the first line of your code, the 0 means the location of the column?

    Also, how can i put the cnt final value and put it in a textbox?

    Thanks for the help...
    2005

  6. #6
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    s = row.Cells(ColumnToSearch).Value

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Now i get this:
    Object reference not set to an instance of an object.

    Error here:
    If s.Contains(StringToFind) Then

  8. #8
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    Well Are you using the StringToFind direcly like that. The StringToFind should be the string you want. Show your code.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Here is what i have:
    vb.net Code:
    1. Dim ColumnToSearch As Integer = 42 'number 42 is the column number as per dataset
    2.         Dim StringToFind As String = "Duedate" 'DueDate is the string i want to be count
    3.         Dim cnt As Integer = 0
    4.         Dim s As String
    5.         For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows
    6.             s = row.Cells(ColumnToSearch).Value
    7.             If s.Contains(StringToFind) Then cnt += 1
    8.         Next
    9.         TotalDueDateTextBox.Text = cnt 'i added this to get the value of the counter
    Thanks for the help!
    Last edited by 2005; Jun 10th, 2007 at 02:07 PM.

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    What is the "StringToFind"? it spouse to be the string you are looking for. For example:
    vb Code:
    1. Dim StringToFind as String = "dog"

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    It is there.
    Dim StringToFind As String = "Duedate"

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    And are you still having the problem?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Yes, i still have the error, does the code seems to be correct?

  14. #14
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    Yes I don’t see any suspicion lines in there. Does the error say what object?

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Nop, just this:
    Object reference not set to an instance of an object and the heading of the error is: NullReferenceException was unhandled by user code.

  16. #16
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    It simms that some times the "s" is null. When you hit the error check the value of "s" to see if it contains a string value. Just point the mouse over "s".

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Yeap, it shows "nothing" and don't know why, i have checked the data and there is no empty cell.

  18. #18
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    I don't know why but if it shows "nothing" then that cell has value of nothing. If you use MSAccess then you can have a conditional line to check if it is nothing or not and then process it.
    vb Code:
    1. Dim ColumnToSearch As Integer = 42
    2.         'number 42 is the column number as per dataset        
    3.         Dim StringToFind As String = "Duedate"
    4.         'DueDate is the string i want to be count        
    5.         Dim cnt As Integer = 0
    6.         Dim s As String
    7.         For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows
    8.             If Not row.Cells(ColumnToSearch).Value = DBNull.Value Then
    9.                 s = row.Cells(ColumnToSearch).Value
    10.                 If s.Contains(StringToFind) Then cnt += 1
    11.             End If
    12.  
    13.         Next
    14.         TotalDueDateTextBox.Text = cnt 'i added this to get the value of the counter

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Sorry but now i got this on the error list:
    Error 1 Operator '=' is not defined for types 'Object' and 'System.DBNull'.

    I do have Access database...
    Thanks.

  20. #20
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    oh my bad, change "=" to "Is".

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    I'm still getting the same issue about the "NullReference..." even with your new code, i will step the code with a breakpoint and post results. It will take me a couple of minutes.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    OK, here is somenew data, i deleted most of my records and left only 7 and when go to stepmode it always do the check for an extra one, in this case 8. The final row in the datagridview will always be empty.
    This line seems to be doing nothing:
    If Not row.Cells(ColumnToSearch).Value Is DBNull.Value Then

  23. #23
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    Replays the
    vb Code:
    1. For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows            
    2. If Not row.Cells(ColumnToSearch).Value = DBNull.Value Then                
    3. s = row.Cells(ColumnToSearch).Value                
    4. If s.Contains(StringToFind) Then cnt += 1            
    5. End If        
    6. Next
    with this
    vb Code:
    1. For Each dr As DataRow In Me.YourDataSet.YourTable.Rows
    2.             s = dr(ColumnToSearch)
    3.             If s.Contains(StringToFind) Then
    4.                 cnt += 1
    5.             End If
    6.         Next

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Ohhhh, that did the trick almost 100%. I'm very happy. The problem with reading the dataset is that it will do a complete count on the table, i mean the complete table, and that table is related to other. In other words, if i have two projects and each project contains multiple rows then the total i will get is the total from the table and not from the project. This is because the datagrid shows the data per project.
    Is there a way to overcome that limitation?

    Thanks for your help...
    2005
    Last edited by 2005; Jun 10th, 2007 at 04:14 PM.

  25. #25
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Total count of string in a DataGridView

    I am not sure that i understand the problem but if you want to go back to the previous code than you can use this:
    vb Code:
    1. For Each row As DataGridViewRow In RecordDataGridView.Rows
    2.             If Not row.IsNewRow Then
    3.                 s = row.Cells("ColumnToSearch").Value
    4.                 If s.Contains(StringToFind) Then
    5.                     cnt += 1
    6.                 End If
    7.             End If
    8.         Next

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Posts
    261

    Re: [2005] Total count of string in a DataGridView

    Now we go to the party! Congrats, you are great... Your last code worked perfect.
    Here it is:

    vb Code:
    1. Dim StringToFind As String = "Completed"
    2.         Dim cnt As Integer = 0
    3.         Dim s As String
    4.         For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows
    5.             If Not row.IsNewRow Then
    6.                 s = row.Cells("DataGridViewTextBoxColumn42").Value
    7.                 If s.Contains(StringToFind) Then
    8.                     cnt += 1
    9.                 End If
    10.             End If
    11.         Next
    12.         TotalDueDateTextBox.Text = cnt

    Thanks for your patience and help.
    2005

  27. #27
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [RESOLVED] [2005] Total count of string in a DataGridView

    You are welcom. Have a good time!

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