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 :(

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()

Edited 5 Years Ago by faroukmuhammad: n/a

Comments
Ooops, missed it.

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'.

Edited 5 Years Ago by __avd: Added [code] tags.

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

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.

Edited 5 Years Ago by Unhnd_Exception: n/a

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

This article has been dead for over six months. Start a new discussion instead.