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