Hi,
I have a grid view(drg).The first column diplays the value of checkbox(true or false).

I am counting the number of "True" values and display it in textbox txtc when I click on it.


Private Sub txtc_Click(ByVal sender As Object, ByVal args As System.EventArgs)
' ** Place Event Handling Code Here **


Dim count As Integer = 0
For Each row As Infragistics.Win.UltraWinGrid.UltraGridRow In dgr.Rows
If row.Cells(0).Value = True Then
count += 1
Else
count += 0
End If
Next
txtc.Text = count.ToString

End Sub


This is the error.

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.InvalidCastException: Conversion from string "DB" to type 'Boolean' is not valid. ---> System.FormatException: Input string was not in a correct format.
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
--- End of inner exception stack trace ---
at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value)
at Microsoft.VisualBasic.CompilerServices.Operators.CompareObject2(Object Left, Object Right, Boolean TextCompare)
at Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(Object Left, Object Right, Boolean TextCompare)
at Script.txtc_Click(Object sender, EventArgs args)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at Infragistics.Win.UltraControlBase.OnClick(EventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Seems that you are missing your quotations for the .Value.

Dim iCount As Integer = 0
        With DataGridView1
            For i As Integer = 0 To .RowCount - 1 '// loop thru all Rows.
                If .Rows(i).Cells(0).Value = "True" Then iCount += 1 '// check for value and Then only add +1.
            Next
        End With
        txtc.Text = iCount.ToString
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.