Count of unique values from one of the column of datagrid in visual studio 2010 RRS feed

  • Question

  • Hi

    I am new on this forum and need help to build the code for count of unique values from one of the column of data grid, database used is MS Access.

    View of grid


    sl no        Priority       Status

    1              Low           New

    2              High          Old

    3             Low           New

    4              Medium      New

    5              Low           New

    6             Low            New

    7             High           Old

    8            Medium       New

    9            Medium       Old

    10          Low             Old

    and it should display the result in a text box in this format

    Low & New = 4

    Medium & New = 1

    High & Old = 2

    Medium & Old = 2

    Low & Old  = 1

    I need Help


    Wednesday, September 26, 2018 5:31 PM

All replies

  • Hello,

    > count of unique values from one of the column

    Low & New = 4

    Low & Old  = 1

    At least you counting on TWO columns.

    > of data grid, database used is MS Access

    In any database you should "count" not on the Grid, but on the table.

    Language to deal with tables named SQL. You have to study it and study deeply enough.

    For your case it could be something like: 

    SELECT     '' + Importance + ' & ' + OldNew AS EXPR2, COUNT(*) AS EXPR1
    FROM         dbo.Table_1
    GROUP BY Importance, OldNew

    Note - This is not an Access SQL, but Access have similar options.

    Sincerely, Highly skilled coding monkey.

    Wednesday, September 26, 2018 6:45 PM
  • If you want to make this easy, use a newer version of Visual Studio and load your DataGridView with a DataTable loaded from the database.

    Code to use in say a button click event. In Select New With we set the fields that are accessible after the LINQ statement has executed.

    Dim dt = CType(DataGridView1.DataSource, DataTable)
    Dim result = From oRow In dt.AsEnumerable()
            Group By PriorityCol = oRow("Priority"), StatusCol = oRow("Status") Into G = Group
            Select New With
                Key .Priority = PriorityCol,
                Key .Status = StatusCol,
                .Value = $"{PriorityCol.ToString()} & {StatusCol.ToString()} = {G.Count()}"
    Dim sb = New Text.StringBuilder
    For Each item In result
    TextBox1.Text = sb.ToString()

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, September 26, 2018 10:49 PM