How can i get a total count of a specific string from an entire column in a datgridview?
Thanks.
2005
Printable View
How can i get a total count of a specific string from an entire column in a datgridview?
Thanks.
2005
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.
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.
Any help will be appreciated!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
2005
Say the column you want to search is the first column.
vb.net Code:
Dim ColumnToSearch As Integer = 0 Dim StringToFind As String = "Hello" Dim cnt As Integer = 0
Now loop through each row and grab the text from the cell.
vb.net Code:
Dim s As String For Each row As DataGridViewRow in myDataGridView.Rows s = row.Cells(ColumnToSearch) If s.Contains(StringToFind) Then cnt += 1 Next
cnt will contain the number of instances found.
I'm getting this error:
Error 1 Value of type 'System.Windows.Forms.DataGridViewCell' cannot be converted to 'String'.
It seems to be here:
From the first line of your code, the 0 means the location of the column?Code:s = row.Cells(ColumnToSearch)
Also, how can i put the cnt final value and put it in a textbox?
Thanks for the help...
2005
s = row.Cells(ColumnToSearch).Value
Now i get this:
Object reference not set to an instance of an object.
Error here:
If s.Contains(StringToFind) Then
Well Are you using the StringToFind direcly like that. The StringToFind should be the string you want. Show your code.
Here is what i have:
Thanks for the help!vb.net Code:
Dim ColumnToSearch As Integer = 42 'number 42 is the column number as per dataset Dim StringToFind As String = "Duedate" 'DueDate is the string i want to be count Dim cnt As Integer = 0 Dim s As String For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows s = row.Cells(ColumnToSearch).Value If s.Contains(StringToFind) Then cnt += 1 Next TotalDueDateTextBox.Text = cnt 'i added this to get the value of the counter
What is the "StringToFind"? it spouse to be the string you are looking for. For example:
vb Code:
Dim StringToFind as String = "dog"
It is there.
Dim StringToFind As String = "Duedate"
And are you still having the problem?
Yes, i still have the error, does the code seems to be correct?
Yes I don’t see any suspicion lines in there. Does the error say what object?
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.
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".
Yeap, it shows "nothing" and don't know why, i have checked the data and there is no empty cell.
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:
Dim ColumnToSearch As Integer = 42 'number 42 is the column number as per dataset Dim StringToFind As String = "Duedate" 'DueDate is the string i want to be count Dim cnt As Integer = 0 Dim s As String For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows If Not row.Cells(ColumnToSearch).Value = DBNull.Value Then s = row.Cells(ColumnToSearch).Value If s.Contains(StringToFind) Then cnt += 1 End If Next TotalDueDateTextBox.Text = cnt 'i added this to get the value of the counter
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.
oh my bad, change "=" to "Is".
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.
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
Replays the
with thisvb Code:
For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows If Not row.Cells(ColumnToSearch).Value = DBNull.Value Then s = row.Cells(ColumnToSearch).Value If s.Contains(StringToFind) Then cnt += 1 End If Nextvb Code:
For Each dr As DataRow In Me.YourDataSet.YourTable.Rows s = dr(ColumnToSearch) If s.Contains(StringToFind) Then cnt += 1 End If Next
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
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:
For Each row As DataGridViewRow In RecordDataGridView.Rows If Not row.IsNewRow Then s = row.Cells("ColumnToSearch").Value If s.Contains(StringToFind) Then cnt += 1 End If End If Next
Now we go to the party! Congrats, you are great... Your last code worked perfect.
Here it is:
vb Code:
Dim StringToFind As String = "Completed" Dim cnt As Integer = 0 Dim s As String For Each row As DataGridViewRow In ProjectDescriptionsDataGridView.Rows If Not row.IsNewRow Then s = row.Cells("DataGridViewTextBoxColumn42").Value If s.Contains(StringToFind) Then cnt += 1 End If End If Next TotalDueDateTextBox.Text = cnt
Thanks for your patience and help.
2005
You are welcom. Have a good time!