
Feb 8th, 2018, 06:00 AM
#1
Thread Starter
Junior Member
datagrid treating empty spaces as zero values
Hi all,
I am trying to get the average of student marks from the database into texboxes on the form(via the datagriid).
I have four fields
name,age,subject,mark
I use the usual method for accessing the database and displaying the data into a datagridview.
I then use the following loop to get the data from DB then average the marks.This code will get the average of the last three marks into a textbox
Code:
Dim sum1 As double
Dim a As single
For a = 0 to 2
sum +=
Datagridview1.Rows(a).Cells("full").Value()
Next
TextBox4.Text = sum/3
This works fine for a student with three marks.
But I want to look at the average for four five six and seven marks and place each one in a seperate TBox.So I just duplicated the above code and changed the varaibles in each one to correspond to the increments.So for example the loop for 5 grades would be this
Code:
Dim sum1 As double
Dim b As single
For b = 0 to 4
sum +=
Datagridview1.Rows(b).Cells("full").Value()
Next
TextBox4.Text = sum/5
So these loops examine the column in the DGV,stop at the rquired amount and then work out an average.
The problem arises when a student may only have 3 grades,the loop however will go up to lets say7 and instead of just ignoring the empty spaces it is treating them as zeros
so imagine the student has 20,45,66 the average would be 43.6,which is correct
But the seven loop is in the DGT looks at it like this,20,45,66,0,0,0,0, and comes up with the average of 18.5.which of course is wrong.
I hope I have explained everything clearly,I am not too sure if I am even doing this right(I am from an Excel baxkground branching out into vb.net) .
Ant suggestions much appreciated.
Kind Regards
al

Feb 8th, 2018, 06:23 AM
#2
Re: datagrid treating empty spaces as zero values
Maybe I'm missing something. Can't you just check the value and don't add it if it is zero\space? Set a counter to know the number of iterations for the average and use that counter.
Please remember next time...elections matter!
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
