Results 1 to 3 of 3

Thread: [2005] DatagridView - automatically sum cells as user selects them

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    64

    [2005] DatagridView - automatically sum cells as user selects them

    Hello,

    Imagine a datagridview with a numeric column named "Duration".
    This datagridview allows multiple selection.

    As user selects multiple cells ONLY from this column, I want label1 to show the sum of all selected durations.
    If user selects another column - or several columns - then label1 should only show "[n] rows selected" (where n represents the number of selected rows)

    I have been googling this but didn't found nothing useful...

    Can you please help?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] DatagridView - automatically sum cells as user selects them

    You'd handle an event that was raised when the user selects a cell. The obvious choice is SelectionChanged. In that event handler you can loop through the SelectedCells collection and test the ColumnIndex of each cell. You can keep running totals of the number of different row indexes and the sum the values. At the end of the loop you can display the appropriate message based on the result.

    Now, try building this up in stages. Try just handling the event first and pop up a message to show that it gets raised at the right times. Next try adding the loop and check that the right cells are present in the collection. Then you can think about adding up sums and counts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    64

    Re: [2005][RESOLVED] DatagridView - automatically sum cells as user selects them

    Hello,

    Did that (under "SelectionChanged" used the "ColumnIndex" property of the cells to evaluate what to do): Worked just fine!

    Easier than I thought!

    Thanks!

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