Results 1 to 3 of 3

Thread: Totalling a count from a grid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    North East (UK)
    Posts
    204

    Question

    Hi All,

    Can anyone tell me how to calculate the total sum from a column in a datagrid and output the result to a textbox. I have a datagrid which retrieves two columns of data for example:

    Type_______Count
    RED _________2
    BLUE_________4
    GREEN________1
    WHITE________2

    The count is done using a query on a SQL server database to retrieve this information. What i need is for the total of the count to be displayed in a separate text box.

    Any Ideas would be appreciated.

    Colin

  2. #2
    Member
    Join Date
    Oct 2000
    Posts
    49
    Colin,

    presumably you are using a recordset as the data source for the datagrid.

    This means you can either

    1/ scroll through the recordset, incrementing the value of a variable, with the value from the count field and when you reach the end of the recordset populate the text box with the variable

    e.g
    dim a as integer

    sql for query.......
    open recordset rs.......
    populate datagrid with recordset....

    a = 0
    rs.movefirst
    do while not rs.eof
    a = a + rs.fields(1).value
    rs.movenext
    loop

    textbox.value = a
    rs.close

    2/ fire another query at the sql database, but just count the affected records, without grouping by type.

    Hope that helps

    Ian

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    North East (UK)
    Posts
    204
    Thats great, thanks for your help

    Colin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width