[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? :)
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.
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! :D
Thanks!