Click here to Skip to main content
14,975,411 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi programmer,
I have code for web:
VB
Private Sub CountHoNgheo()
        Try
            Dim HoN As Integer = 0
            For i As Integer = 0 To dgInput.Rows.Count - 1
                If dgInput.Rows(i).Cells(6).Text Like "ABC" Then
                    HoN = HoN + 1
                End If
            Next
            lblNgheo1.Text = HoN.ToString
        Catch ex As Exception
            lblMessage.Text = ex.ToString
        End Try
End Sub


The result is 0. (In fact, result = 2)
Cell index number is correct.
Also, with this code in application, applied to DataGridView, the result is correct.

Best thank
Posted
Updated 10-Feb-15 20:56pm
v3
Comments
Maciej Los 11-Feb-15 3:22am
   
What's datasource?
thuydao 11-Feb-15 4:30am
   
I'm using access.

Ok, I've done it. This is my code:
VB
Private Sub CountHoNgheo()
        Try
            Connect()
            Dim ds As New DataSet
            ds.Clear()
            Dim da As New OleDbDataAdapter("SELECT * from tblKH WHERE(((tbl.A) = 'ABC'))", cn)
            lblNgheo1.Text = da.Fill(ds, "tbl").ToString
        Catch ex As Exception
            lblMessage.Text = ex.ToString
        End Try
End Sub


Thank for your suggestion!
   
Comments
Shwrv 14-Apr-16 0:16am
   
@thuydao. thnx. helpful solution.
Instead of using for - loop on the gridview, use SQL query as is shown below:
SQL
SELECT COUNT(*)
FROM TableName
WHERE FieldName Like "ABC*"


For further information, please see:
Walkthrough: Editing an Access Database with ADO.NET[^]
ADO.NET Code Examples[^]
Using ADO.NET for beginners[^]
   
Comments
thuydao 11-Feb-15 19:59pm
   
Dear,
I want to show the counting result in label or textbox (not in GridView).
Thanks
Maciej Los 12-Feb-15 1:44am
   
What's the difference? Control is control no matter of its name.
thuydao 12-Feb-15 2:51am
   
I solved my problem by using SQL query db directly.
But I want to query in GridView because my project is:
- Search item (with criteria) and show in GridView
- Then, Count item in cells of GridView.
If I using SQL query, it will show total of all items without filtering
Thanks,
Maciej Los 12-Feb-15 3:23am
   
I think you should use Linq to achieve that.
thuydao 12-Feb-15 3:27am
   
please introduce about Linq.
Thanks
Maciej Los 12-Feb-15 6:24am
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900