By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

Sum column of a DataGridVuew

P: n/a
I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony
Oct 30 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tony,

Have a look at the datacolumn.compute statement, much easier in my idea

http://msdn2.microsoft.com/en-us/lib...e.compute.aspx

I hope this helps,

Cor

"Tony A." <To***@discussions.microsoft.comschreef in bericht
news:C6**********************************@microsof t.com...
I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is
blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony

Oct 30 '06 #2

P: n/a
It seems like you do the check on column 5 but sum column 6.

//Christer

"Tony A." <To***@discussions.microsoft.comwrote in message
news:C6**********************************@microsof t.com...
I'm trying to write a procedure that sums the values of a column in a
datagridview. If a row is empty (has no value entered) the procedure
crashes. I've added code to check to see if the column for the row is
blank,
this error apears:

Conversion from type 'DBNull' to type 'Integer' is not valid.

The code that I've written is: (the variables are defined)

Do Until dgvTblService.CurrentCell.RowIndex = varRowCount
varCol = dgvTblService.CurrentCell.ColumnIndex
varRow = dgvTblService.CurrentCell.RowIndex

If IsDBNull(Me.dgvTblService.CurrentRow.Cells(5)) Then
Me.dgvTblService.CurrentRow.Cells(5).Value =
Convert.ToDouble(0)
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
Else
varSumHours +=
CInt(Me.dgvTblService.CurrentRow.Cells(6).Value)
Me.dgvTblService.CurrentCell = Me.dgvTblService(varCol,
varRow + 1)
End If
Loop

Any suggestions on what I'm doing wrong?
--
Tony

Oct 31 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.