Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagridviewcheckboxcell truevalue property

Status
Not open for further replies.

tonydough

Programmer
Sep 14, 2009
2
US
I have a dataset containing 2 tables, one for vendors and one for vouchers. These are linked together and bound to 2 datagridviews. Each vendor can have more than one voucher associated with it.

I have an "approved" checkbox column in the voucher table. I want to have a custom approved column set up in the vendor table so that when a vendor is checked, all its associated vouchers are and vice versa. When a voucher is checked, it looks to see if I need to set the vendor checkbox to checked, unchecked or partial (unknown). For this I am using a DataGridViewCheckBoxCell set up like this:

.TrueValue = LightStatus.TurnedOn
.FalseValue = LightStatus.TurnedOff
.IndeterminateValue = LightStatus.Unknown
.ThreeState = True
.ValueType = GetType(LightStatus)

(as per MSDS example).

This column (colApproved) is the only one that isn't databound because it has slightly different behaviour.


grdVendors.Item("colApproved", grdVendors.CurrentCell.RowIndex).Value = LightStatus.TurnedOn

When I implement this I get a runtime datagrid error that says its an invalid cast from LightStatus to CheckState. What am I missing here?

Any help would be appreciated.


Below is the full code I'm using:

Private Sub grdCurrent_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdVouchers.CellContentClick, grdVendors.CellContentClick
Dim grdCurrent As DataGridView = DirectCast(sender, DataGridView)
If TypeOf grdCurrent.CurrentCell Is DataGridViewCheckBoxCell Then

Dim celCheckBox As DataGridViewCheckBoxCell = DirectCast(grdCurrent.CurrentCell, DataGridViewCheckBoxCell)

With Payments.Vendors(CurrentVendorID)

If grdCurrent Is grdVendors Then
' Set all the associated vouchers to be the same as the current vendor
For intVoucher As Integer = 0 To .Vouchers.List.Count - 1
.Vouchers(intVoucher).Approved = celCheckBox.Value
' Update the voucher grid to represent the new value
grdVouchers.Item("approved", intVoucher).Value = celCheckBox.Value
Next
ElseIf grdCurrent Is grdVouchers Then
.Vouchers(CurrentVoucherID).Approved = celCheckBox.Value
' Update the vendor checkbox cell accordingly
If .Vouchers.AllApproved Then
grdVendors.Item("colApproved", grdVendors.CurrentCell.RowIndex).Value = LightStatus.TurnedOn
ElseIf .Vouchers.AllNotApproved Then
grdVendors.Item("colApproved", grdVendors.CurrentCell.RowIndex).Value = LightStatus.TurnedOff
Else
grdVendors.Item("colApproved", grdVendors.CurrentCell.RowIndex).Value = LightStatus.Unknown
End If
End If

End With

End If
End Sub

And below here is the stacktrace:

System.FormatException: Invalid cast from 'LightStatus' to 'System.Windows.Forms.CheckState'. ---> System.InvalidCastException: Invalid cast from 'LightStatus' to 'System.Windows.Forms.CheckState'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.Enum.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
--- End of inner exception stack trace ---
at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)
at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
 
I use the following code to determine a checkbox's value in my datagrid. Perhaps you could adapt it to use a "For" loop to navigate the voucher's rows and look for appropriate records to check (or uncheck).

Code:
    Private Sub dgMaint_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgMaint.CellContentClick
        Try
            With Me.dgMaint
                If .CurrentRow IsNot Nothing Then
                    ' the first column contains the checkbox
                    If e.ColumnIndex = 0 Then
                        If .Item(0, e.RowIndex).Value = True Then ' the checkbox is checked
                            .Item(0, e.RowIndex).Value = False ' uncheck the checkbox
                            MaintRowsSelected -= 1
                            ' maintain the CheckAll Checkbox
                            If chkAllMaint.Checked Then
                                MaintManualDeselecting = True
                                chkAllMaint.Checked = False
                                MaintManualDeselecting = False
                            End If
                        Else ' the checkbox is UNChecked
                            .Item(0, e.RowIndex).Value = True ' check the checkbox
                            MaintRowsSelected += 1
                            ' Maintain the CheckAll checkbox
                            If MaintRowsSelected = Me.dgMaint.RowCount Then
                                MaintManualDeselecting = True
                                chkAllMaint.Checked = True
                                MaintManualDeselecting = False
                            End If
                        End If

                        UpdateMaintDataGrid()
                    End If
                End If
            End With
        Catch ex As Exception
            DisplayException(ex)
        End Try
    End Sub

    Private Sub UpdateMaintDataGrid()
        Try
            Me.dgMaint.Refresh()

            If MaintRowsSelected > 0 Then
                Me.btnProcessMaint.Enabled = True
            Else
                Me.btnProcessMaint.Enabled = False
            End If

            With Me.lblMaintStatus
                .Text = "Rows: " & oMainsaver.Count & " Selected: " & MaintRowsSelected
                .Visible = True
            End With
        Catch ex As Exception
            Throw
        End Try
    End Sub

HTH

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
Thanks for your feedback but it's not quite my problem.
I have no problem setting a checkbox in a datagrid to be on or off, its more to do with it's ThreeState property.

.Item(0, e.RowIndex).Value = False

The line in your code here unchecks the box, how do i set the box to be the intermediate value?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top