none
count non duplicate row RRS feed

  • Question

  • hi

    I want a total rows of  column 4  where duplicate text values are excluded

    datagridview example data

    alie

    samire

    alie

    maher

    count = 3

    Friday, November 30, 2018 3:17 AM

Answers

  • Hi,

    You can also not fill DataTable,

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim i = 0
            Using conn = New SqlConnection(constr)
                conn.Open()
                cmd = New SqlCommand("Select distinct ColumnName From TableName ", conn)
                Dim read As SqlDataReader = cmd.ExecuteReader
                While read.Read
                    i += 1
                End While
            End Using
            MsgBox(i)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Monday, December 3, 2018 7:31 AM
    Monday, December 3, 2018 3:11 AM

All replies

  • Check this:

    Dim count = DataGridView1.Rows.Cast(Of DataGridViewRow).Where(Function(r) Not r.IsNewRow).Select(Function(r) r.Cells(4).Value).Distinct.Count

    Friday, November 30, 2018 5:48 AM
  • Error 1 'Cast' is not a member of 'System.Windows.Forms.DataGridViewRowCollection'.
    I USED VB 2010
    • Edited by ahmeddc Friday, November 30, 2018 5:52 AM
    Friday, November 30, 2018 5:52 AM
  • Hi,

    try the code:

      Dim lsName As List(Of String) = New List(Of String)()
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim name As String = Me.DataGridView1.Rows(i).Cells(1).Value.ToString()
    
                If lsName.Contains(name) Then
                    Continue For
                Else
                    lsName.Add(name)
                End If
            Next
    
            MsgBox(lsName.Count)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 30, 2018 6:08 AM
  • Hi,

    try the code:

      Dim lsName As List(Of String) = New List(Of String)()
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For i As Integer = 0 To DataGridView1.Rows.Count - 1
                Dim name As String = Me.DataGridView1.Rows(i).Cells(1).Value.ToString()
    
                If lsName.Contains(name) Then
                    Continue For
                Else
                    lsName.Add(name)
                End If
            Next
    
            MsgBox(lsName.Count)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    THANKS

    The code works well
    Is there a method for counting FROM DIRECT DATASET ACCESS DATABASE?

    Friday, November 30, 2018 6:20 AM
  • Hi,

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select distinct ColumnName  From TableNmae ", conn)
                dt = New DataTable()
                sda.Fill(dt)
                MsgBox(dt.Rows.Count)
            End Using

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 30, 2018 6:51 AM
  • Hi,

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select distinct ColumnName  From TableNmae ", conn)
                dt = New DataTable()
                sda.Fill(dt)
                MsgBox(dt.Rows.Count)
            End Using

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

     

    Excellent  Alex


    The last question in the same context
    If you want to perform the process for two different columns
    Is it possible to do this or will be tired Datatabel FILL Twice

    Friday, November 30, 2018 11:53 AM
  • Hi,

    You can also not fill DataTable,

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim i = 0
            Using conn = New SqlConnection(constr)
                conn.Open()
                cmd = New SqlCommand("Select distinct ColumnName From TableName ", conn)
                Dim read As SqlDataReader = cmd.ExecuteReader
                While read.Read
                    i += 1
                End While
            End Using
            MsgBox(i)
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by ahmeddc Monday, December 3, 2018 7:31 AM
    Monday, December 3, 2018 3:11 AM