Count datagridview rows where value not equal to zero

PDS8475

Active member
Joined
Jun 25, 2019
Messages
41
Programming Experience
Beginner
Hi
I have a datagridview which shows the results of searches. One of the datagridview columns is called price. I have a textbox showing the total of this column. But to do this I had to tell the program to put 0 in records where this field is empty. Now I want to count the records where this field is not 0 but I'm uncertain how to do this. Looking on google I can find examples of counting the total rows of a datagridview and also how to count rows with a specific value in a column. But on mine the value is different on each row. I suppose I could count how many rows are at 0 and minus that from a total row count but that seems like a long way to do what I want.
 
How was the grid populated in the first place? If it's bound to a DataTable, you should simply call the Compute method of that table with the appropriate expression.
But to do this I had to tell the program to put 0 in records where this field is empty.
No you didn't. You just had to check whether a cell was empty first and then not include it in the sum if it is.
I can find examples of counting the total rows of a datagridview and also how to count rows with a specific value in a column. But on mine the value is different on each row. I suppose I could count how many rows are at 0 and minus that from a total row count but that seems like a long way to do what I want.
It's the exact same principle but, instead of adding 1 to a count, you add the cell value to a sum.

It's not really for us to write code from scratch in situations like this. You almost certainly have the vast majority of what you need, so show us what you have and point out where you think it needs to change. We can then either show you the change so you can copy and paste or we can explain the principle and you can figure out the change to make from that. Either way, the relevant code should always be posted with a question like this.
 
Furthermore, you should not be using your View (e.g. your UI) as part of you Model. The View just represents what is is your data model. You need to do all your logic against the model, not the view. So that means that you should be counting the rows that satisfy your criteria in your data -- presumably the DataTable that you bound to your DataGridView.
 
Back
Top Bottom