|
-
Jun 10th, 2007, 12:03 PM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 10th, 2007, 12:06 PM
#2
Hyperactive Member
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.
-
Jun 10th, 2007, 12:12 PM
#3
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2007, 12:18 PM
#4
Hyperactive Member
Re: [2005] Total count of string in a DataGridView
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.
Prefix has no suffix, but suffix has a prefix.
-
Jun 10th, 2007, 01:07 PM
#5
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2007, 01:19 PM
#6
Re: [2005] Total count of string in a DataGridView
s = row.Cells(ColumnToSearch).Value
-
Jun 10th, 2007, 01:33 PM
#7
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2007, 01:56 PM
#8
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.
-
Jun 10th, 2007, 02:04 PM
#9
Thread Starter
Hyperactive Member
Re: [2005] Total count of string in a DataGridView
Here is what i have:
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
Thanks for the help!
Last edited by 2005; Jun 10th, 2007 at 02:07 PM.
-
Jun 10th, 2007, 02:06 PM
#10
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:
Dim StringToFind as String = "dog"
-
Jun 10th, 2007, 02:09 PM
#11
Thread Starter
Hyperactive Member
Re: [2005] Total count of string in a DataGridView
It is there.
Dim StringToFind As String = "Duedate"
-
Jun 10th, 2007, 02:13 PM
#12
Re: [2005] Total count of string in a DataGridView
And are you still having the problem?
-
Jun 10th, 2007, 02:17 PM
#13
Thread Starter
Hyperactive Member
Re: [2005] Total count of string in a DataGridView
Yes, i still have the error, does the code seems to be correct?
-
Jun 10th, 2007, 02:20 PM
#14
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?
-
Jun 10th, 2007, 02:25 PM
#15
Thread Starter
Hyperactive Member
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.
-
Jun 10th, 2007, 02:29 PM
#16
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".
-
Jun 10th, 2007, 02:39 PM
#17
Thread Starter
Hyperactive Member
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.
-
Jun 10th, 2007, 02:45 PM
#18
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:
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
-
Jun 10th, 2007, 02:54 PM
#19
Thread Starter
Hyperactive Member
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.
-
Jun 10th, 2007, 02:56 PM
#20
Re: [2005] Total count of string in a DataGridView
oh my bad, change "=" to "Is".
-
Jun 10th, 2007, 03:12 PM
#21
Thread Starter
Hyperactive Member
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.
-
Jun 10th, 2007, 03:32 PM
#22
Thread Starter
Hyperactive Member
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
-
Jun 10th, 2007, 03:51 PM
#23
Re: [2005] Total count of string in a DataGridView
Replays the
vb 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
Next
with this
vb Code:
For Each dr As DataRow In Me.YourDataSet.YourTable.Rows
s = dr(ColumnToSearch)
If s.Contains(StringToFind) Then
cnt += 1
End If
Next
-
Jun 10th, 2007, 04:10 PM
#24
Thread Starter
Hyperactive Member
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.
-
Jun 10th, 2007, 04:22 PM
#25
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:
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
-
Jun 10th, 2007, 04:37 PM
#26
Thread Starter
Hyperactive Member
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:
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
-
Jun 10th, 2007, 04:43 PM
#27
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|