I am making databases and one of columns is bit(checked or not). an want to sum all the checked ones number field.how to find If is checked or not?


Dim sum As Single = 0.0


For i = 0 To Table1DataGridView.RowCount - 1
sum += Table1DataGridView.Rows(i).Cells(4).Value()

this is just how to sum but I want to sum if field is checked. please help :(

Recommended Answers

All 12 Replies

any ideas?

wouldn't an if between the for and the sum do the trick?

For i = 0 To Table1DataGridView.RowCount - 1
if Table1DataGridView.Rows(i).Cells(3).Value() = "True"  sum += Table1DataGridView.Rows(i).Cells(4).Value()

adams_k is right' if only he had added "then" to line 2 like this;

For i = 0 To Table1DataGridView.RowCount - 1
if Table1DataGridView.Rows(i).Cells(3).Value() = "True" Then sum += Table1DataGridView.Rows(i).Cells(4).Value()
commented: Ooops, missed it. +5

Hmm the rite 1 ..

thanks for response but still problem : Operator '=' is not defined for type 'DBNull' and string "True".

Dim sum As Single = 0.0

        For i = 0 To Table1DataGridView.RowCount - 1
            If Table1DataGridView.Rows(i).Cells(3).Value() = "True" Then sum += Table1DataGridView.Rows(i).Cells(5).Value()
        Next
    End Sub

Error: Operator '=' is not defined for type 'DBNull' and string "True".

Dim sum As Single = 0.0

        For i = 0 To Table1DataGridView.RowCount - 1
            If Table1DataGridView.Rows(i).Cells(3).Value() = True Then sum += Table1DataGridView.Rows(i).Cells(5).Value()
        Next

Error:Operator '=' is not defined for type 'DBNull' and type 'Boolean'.

dnt use True in Quotes

I've done both of them with and without and see errors in previous post

Member Avatar for Unhnd_Exception

Cell(3).value doesn't have a boolean value in it. It is null. It has no value. You need to check if it has a value first

If cell(3).value isnot nothing andalso Typeof Cell(3).value is boolean then
    if cbool(cell(3).value,boolean) then
        += the sum
       'before adding the sum check that the type of cell(4)
       'if cell(4).value isnot nothing andalso isnumeric(cell(4).value) then
       '+= cint(cell(4).value)
    endif 
endif

Make sure you have your column numbers correct if you feel that cell(3) should have a value of boolean in it.

Member Avatar for Unhnd_Exception

After looking over the code I posted there is a type o with the if cbool()

it should be:

if cbool(cell(3).value) then

And make sure all rows for the bit column in your table have a value set.

Cell(3).value doesn't have a boolean value in it. It is null. It has no value. You need to check if it has a value first

If cell(3).value isnot nothing andalso Typeof Cell(3).value is boolean then
    if cbool(cell(3).value,boolean) then
        += the sum
       'before adding the sum check that the type of cell(4)
       'if cell(4).value isnot nothing andalso isnumeric(cell(4).value) then
       '+= cint(cell(4).value)
    endif 
endif

Make sure you have your column numbers correct if you feel that cell(3) should have a value of boolean in it.

thank a lot. It worked well but only instead of if cbool(cell(3).value,boolean) then
I wrote if cbool(cell(3).value) then

Thank all works perfect :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.