Home All Groups Group Topic Archive Search About

Sum column of a DataGridVuew

Author
30 Oct 2006 3:43 PM
Tony 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

Author
30 Oct 2006 5:40 PM
Cor Ligthert [MVP]
Tony,

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

http://msdn2.microsoft.com/en-us/library/system.data.datatable.compute.aspx

I hope this helps,

Cor

Show quoteHide quote
"Tony A." <To***@discussions.microsoft.com> schreef in bericht
news:C68716F3-608A-4928-AA77-3DFD451ECE0A@microsoft.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
Author
31 Oct 2006 12:44 PM
Christer Sörell
It seems like you do the check on column 5 but sum column 6.

//Christer

Show quoteHide quote
"Tony A." <To***@discussions.microsoft.com> wrote in message
news:C68716F3-608A-4928-AA77-3DFD451ECE0A@microsoft.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