This is a migrated thread and some comments may be shown as answers.

radGrid, edit form, insert, edit, checkbox

1 Answer 210 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Paula
Top achievements
Rank 1
Paula asked on 17 Aug 2010, 10:04 PM

I keep getting the dredded "Conversion from type 'DBNull' to type 'Boolean' is not valid." error and cannot seem to get around it. 

I was getting it when I clicked on Add new record and worked around that by adding default values in code behind:

If (e.CommandName = Telerik.Web.UI.RadGrid.InitInsertCommandName) Then
    'cancel the default operation
    e.Canceled = True
    'Prepare an IDictionary with the predefined values
    Dim newValues As System.Collections.Specialized.ListDictionary = New System.Collections.Specialized.ListDictionary()
    'set default checked state for checkbox inside the EditItemTemplate
    newValues("Is_Admin") = False
    newValues("Is_Disabled") = False
    'Insert the item and rebind
    e.Item.OwnerTableView.InsertItem(newValues)
End If

But now if I'm in insert mode, so I have clicked on Add New Record then without cancelling insert click on edit I get the error again.  If I cancel the insert by clicking cancel then the error does not appear.  Is there a way to cancel the insert in code behind before the grid is put in edit mode to avoid this error?

Paula

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 18 Aug 2010, 06:39 AM
Hello Paula,

Try to use the following code for CheckBox to avoid that error.

ASPX:
<asp:CheckBox ID="CheckBox1" Checked='<%# iif(DataBinder.Eval(Container.DataItem,"Is_Admin") is DBNull.Value,false,Eval("Is_Admin")) %>' runat="server" />

Also refer the following forum which describes the similar scenario.
asp:Checkbox in FormTemplate. "Specified Cast is Invalid"

If you want to cancel the insert operation when performing edit , try the following code.

VB.Net:
Protected Sub RadGrid1_ItemCommand(source As Object, e As GridCommandEventArgs)
    If e.CommandName = RadGrid.EditCommandName Then
        If RadGrid1.MasterTableView.IsItemInserted Then
            e.Canceled = True
        End If
    End If
End Sub

Hope this helps,
Princy.
Tags
Grid
Asked by
Paula
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or