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

Bind checkbox in Template Column

25 Answers 1767 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jimmy Stuart
Top achievements
Rank 1
Jimmy Stuart asked on 05 Mar 2008, 05:34 AM

I have a weird issue that appears to have cropped up after switching from a regular RadGrid to the Prometheus Rad Grid (3.1425 release).  I have several check boxes in a template column and am using the automatic databinding:

<asp:CheckBox ID="PublicCheckBox" Checked='<%#Bind("IsPublic") %>' runat="server" />

This worked fine on the old grid.  But on the new Prometheus one I get the following error:

Conversion from type 'DBNull' to type 'Boolean' is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid.

Source Error:

Line 173:							<br />
Line 174:							Is Public?
Line 175: <asp:CheckBox ID="PublicCheckBox" Checked='<%#Bind("IsPublic") %>' runat="server" />Line 176:							<br />
Line 177:							Folder:


What's weird is that this only occurs when I try to insert a record.  Editing an existing record works fine.  There are a total of 3 Boolean columns in the database table ALL of which do NOT allow Null and have a default value of False (or 0).  Also this error occurs whether or not the grid is Ajaxified with the Prometheus RadAjaxManager.  Again, I'm using all automatic datasource operations and the datasource controls and the data is the same that worked just fine with the old Rad Grid.  Is there anything in the new grid that would cause this?

Thanks,

Jimmy

25 Answers, 1 is accepted

Sort by
0
plamen
Top achievements
Rank 1
answered on 07 Mar 2008, 05:50 AM
hi



You have to set predefine value on init insert to avoid the problem. Review the last paragraph of this documentation topic:


http://www.telerik.com/help/aspnet/grid/?grdInsertingValuesInPlaceAndEditForms.html


Regards...
<John:Peel />
0
Simon
Top achievements
Rank 2
answered on 13 May 2008, 02:36 PM

I have this same problem.  John's solution worked.  I added this code-behind:

Partial

Public Class BusinessAssociateList

Inherits System.Web.UI.Page

Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand

If (e.CommandName = 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 value for the dropdown list inside the EditItemTemplate

newValues("Status") = "ACTIVE"

'set default checked state for checkbox inside the EditItemTemplate

newValues("DunnsPrimaryFlag") = True

'Insert the item and rebind

e.Item.OwnerTableView.InsertItem(newValues)

End If

End Sub

End

Class


Notice how this is the ONLY code behind my asp page.  Everything is declarative, except the default values for the newly inserted item.  It seems like this is a bug or missing feature in the new grid.  Isn't it?  I have a medium complexity grid, pulling data from an objectdatasource, and it is editable using a Template form with drop downs and a checkbox.  The checkbox is the only control that errors.  And this code behind solves the problem.  However, this code behind seems to be a work around. 
I think the GridColumn should have a DefaultValue propery that can be set declaritively and which is then used to initialize newly inserted rows.

0
jonnyO
Top achievements
Rank 1
answered on 29 Jun 2008, 04:21 AM
Cheers to you Simon... like the DbSelectDate for DatePicker...
0
Steve Newbery
Top achievements
Rank 1
answered on 22 Aug 2008, 09:05 AM

"The checkbox is the only control that errors.  And this code behind solves the problem.  However, this code behind seems to be a work around. 
I think the GridColumn should have a DefaultValue propery that can be set declaritively and which is then used to initialize newly inserted rows."

I completely agree - this seems a "kludge" and is a bit annoying really. Can you please consider this for the next release of radGrid?

0
Iana Tsolova
Telerik team
answered on 22 Aug 2008, 10:55 AM
Hi Steve Newbery,

Have you tried using grid specific column instead of GridTemplate column? For instance, GridCheckBoxColumn, GridDropDownColumn, GridDateTimeColumn, Give it a try and check if the problem persists.
Find more about RadGrid column type here.

Greetings,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Steve Newbery
Top achievements
Rank 1
answered on 22 Aug 2008, 06:33 PM
Iana, yes I have, and using GridCheckBoxColumn I don't have this problem.

But, I have to refer to the column's ID from a validator, and I don't see how to do this when the column is a grid specific column.

So if I have:

<telerik:GridCheckBoxColumn UniqueName="MailExtract" HeaderText="MailExtract" 
      DataField="MailExtract">  
</telerik:GridCheckBoxColumn> 

what then is the ID of the control? I need to refer to it in my validator: 

<des:RequiredTextValidator runat="server" 
    ID="EmailRequiredTextValidator"   
    ControlIDToEvaluate="EmailTextBox"   
    ErrorMessage="This field is required if MailExtract is checked">  
    <EnablerContainer> 
        <des:CheckStateCondition ControlIDToEvaluate="MailExtractCheckBox" /> 
    </EnablerContainer> 
</des:RequiredTextValidator> 
 

Thanks for any help, Steve
0
Sebastian
Telerik team
answered on 25 Aug 2008, 11:57 AM
Hi Steve,

Thank you for the additional information.

You may consider assigning the ControlIDToEvaluate property of your custom validator dynamically intercepting the ItemCreated event of the grid. The code implementation from the first section of this help topic can get you started:

http://www.telerik.com/help/aspnet-ajax/grdvalidation.html

Let me know if I am missing something from your logic.

Best regards,
Stephen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Carl
Top achievements
Rank 1
answered on 01 Dec 2008, 12:38 AM
I just experienced the exact problem described in the first post in this thread. A checkbox in a GridTemplateColumn. Works great when editing existing records but bombs when inserting a new record, again with the same error as described in the first post of this thread.

So I agree. It seems an unnecessary added complexity to be obligated to write some code-behind just to handle some simple defaults for a boolean on a checkbox that easily could be set to default to false by the Telerik assembly. Please add my vote to the other requests to fix this problem in the next release of RadGrid.

In general, all of the controls in the Telerik RadGrid assembly should have basic defaults like this already built in. It does not make sense to have this great wonderful powerful RadGrid, and then have to waste time tracking down errors (and/or writing extra unnecessary code-behind) just to handle missing defaults that should be automatically built into the released Telerik assembly.

Thank you for prioritizing for next release.

CT
0
Sebastian
Telerik team
answered on 01 Dec 2008, 08:05 AM
Hello Carl,

Thank you for your feedback - I will forward it to our dev team to be considered for the future versions of the product. Additionally, I will appreciate if you provide some pointers about online resources which elaborate on this subject or other web grids which has the requested functionality built-in. Thus we will be able research the matter in more detail.

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Carl
Top achievements
Rank 1
answered on 01 Dec 2008, 05:55 PM
I do not understand the request for pointers on this matter to other webgrids etc that have the desired functionality.

This matter is fundamental and basic. If any developer creates any web form of any kind, then the web form should be designed so that it initializes without bombing out because of some silly error that results from a variable or class not being initialized to a reasonable and sensible initial default value. So for booleans, just initialize to false if that's what makes sense, but do not leave it uninitialized (null or nothing) so that the whole web form blows up. This matter is that simple. There's no need to research anything here.

Conclusion: controls for web forms designed and developed for release with the Telerik assembly should be properly coded with appropriate initial default values so that the web forms do NOT blow up!!!
0
Sebastian
Telerik team
answered on 02 Dec 2008, 09:13 AM
Hi Carl,

Thank you for the additional explanation - your feedback has already been forwarded to our RadGrid development department to be considered for the upcoming versions of the product. In the meantime I hope that setting a predefined value for the boolean field intercepting the init insert command is feasible for your situation.

Kind regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
jimmie
Top achievements
Rank 1
answered on 15 Jun 2009, 05:52 PM
When i tried this code it threw an exception 

Conversion from type 'DBNull' to type 'Boolean' is not valid.

At the insert line

 

 

 

 

 

e.Item.OwnerTableView.InsertItem(newValues)

0
Keith Heilveil
Top achievements
Rank 1
answered on 30 Oct 2009, 02:55 PM
Yes I'm reviving this thread. I vote it be fixed, as the developer should be able to expect the controls to at least set their default values without bombing on an Add.
0
Shawn Krivjansky
Top achievements
Rank 1
answered on 10 May 2010, 04:11 AM
Another vote to fix.
Just wasted an hour chasing this around.
FYI, I am on the 2010 Q1 415 (SP1) release, so it hasn't been fixed to this point.
0
Shawn Krivjansky
Top achievements
Rank 1
answered on 10 May 2010, 04:22 AM

Actually, just found the missing property that gets around this...didn't notice it before.
On GridTemplateColumn:
"DefaultInsertValue"
This will make things NOT blow up like it had previously (without the property set).

 

<telerik:GridTemplateColumn HeaderText="TM" HeaderButtonType="TextButton" 
                            DataField="isTaskManager" UniqueName="isTaskManager"   
                            SortExpression="isTaskManager" 
                            HeaderStyle-Width="25" DataType="System.Boolean" DefaultInsertValue="False"

 

 

 


 

0
Joshua Warren
Top achievements
Rank 1
answered on 10 Jun 2010, 05:24 PM
I tried this, and for the most part it worked.  The only problem is that when the add button/text is clicked, it goes to the last page in the pagination.  All of the controls that are supposed to come up do, it just goes to the last page.  Is there any way to keep this from happening??  Can I capture the page that I am on when the add button is pressed, and then set it back to that page?

Thanks in advance,

-- joshua
0
Joshua Warren
Top achievements
Rank 1
answered on 10 Jun 2010, 05:30 PM
Also, if you are in the adding mode, and you try to change pages, it will then throw the original error of "Conversion from type 'DBNull' to type 'Boolean' is not valid. 
0
Shinu
Top achievements
Rank 2
answered on 11 Jun 2010, 07:02 AM
Hello Joshua,

Try the following code snippet and see whether this helps you.

ASPX:
<telerik:GridTemplateColumn> 
     <EditItemTemplate> 
          <asp:CheckBox ID="CheckBox1" Checked='<%# (DataBinder.Eval(Container.DataItem,"isapproved") is DBNull ?false:Eval("isapproved")) %>' runat="server" /> 
     </EditItemTemplate> 
 </telerik:GridTemplateColumn> 

Regards,
Shinu.



0
Joshua Warren
Top achievements
Rank 1
answered on 14 Jun 2010, 01:57 PM

Shinu,

   Thank you for your response.  It didn't quite work, but got me on the right path.  I ended up finding some code that worked:

<asp:CheckBox ID="chbx_Active" runat="server" checked='<%# IF(Eval("lp_active") is DBNull.Value, False, Eval("lp_active")) %>'/>  
 

 

Thanks, again!

-- Joshua

 

 

 

 


 

 

0
sroussos
Top achievements
Rank 2
answered on 01 Dec 2012, 01:18 PM
Worked for me!
0
Angel Petrov
Telerik team
answered on 01 Oct 2015, 02:05 PM
Hello guys,

I am happy to announce that we have integrated an improvement regarding this matter. With the latest 2015 Q3 official release an exception will not be thrown. Instead the default value of the boolean field will be set to false instead of DBNull.

Regards,
Angel Petrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sonny
Top achievements
Rank 1
answered on 02 Oct 2015, 02:06 PM

I have downloaded the Telerik_UI_for_ASP.NET_AJAX_Setup_2015_3_930.zip file and cannot extract it. I am getting an error that says it is an invalid archive.

I cannot use the .exe or the .msi files because I do not have local admin rights on the development computer. 

 

0
Marin Bratanov
Telerik team
answered on 05 Oct 2015, 04:57 AM

Hello Sonny,

I would suggest downloading the file again, the most likely reason for such a problem is a connectivity issue that has prevent the zip from downloading properly.

Alternatives I can suggest are:

  • see if you can install the Telerik Control Panel on you machine as it can help with downloads/installations
  • consider reaching out to your network administrators so they can assist with ensuring the archive is downloaded properly
  • look for the "hotfix" archive that contains only the dlls, scripts, skins and resources as it is smaller and it is more likely to download successfully
  • open a support ticket so we can send you the assemblies at least until you can get them from our site

On a side note - I would suggest opening separate threads for separate issues. Adding posts that are not related to the original subject is likely to dilute the thread and make information harder for everyone to find in it.

Regards,

Marin Bratanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Neha
Top achievements
Rank 1
answered on 08 Feb 2016, 10:43 AM

Hi John,

 I have an issue with checkbox in radgrid. I have a checkbox defined in radgrid with a server side event :

<asp:CheckBox ID="CheckBox1" runat="server" Commandname="RowClick" OnCheckedChanged="ToggleRowSelection"
AutoPostBack="True" />

This changes the row color when the corresponding checkbox is clicked.

This code was working fine till I upgraded from .net 2.0 to .net 4.0

Now this gives an error while rendering the checkbox. I am attaching screenshot of error.

This error goes away when I remove the autopostback = true from the checkbox control. But then my server side event to toggle row selection is not fired.

Following is the code snippet.

  <radG:RadGrid ID="RadGrid1" runat="server" EnableAJAX="true" EnableAJAXLoadingTemplate="true"
AllowFilteringByColumn="false" LoadingTemplateTransparency="30" Skin="Outlook2007"
Width="570px" PageSize="7" AllowSorting="True" AllowMultiRowSelection="True"
AllowPaging="True" AllowCustomPaging="True" ShowGroupPanel="false" GridLines="None"
AutoGenerateColumns="False" Height="230px" SkinsPath="../RadControls/Grid/Skins"
OnItemCommand="RadGrid1_ItemCommand">
<PagerStyle Mode="NumericPages" Position="Bottom" CssClass="GridPager"></PagerStyle>
<MasterTableView DataKeyNames="FacilityId" DataMember="Hotels" AllowMultiColumnSorting="False"
Width="100%" TableLayout="Fixed" Name="Main">
<Columns>
<radG:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Commandname="RowClick" OnCheckedChanged="ToggleRowSelection"
AutoPostBack="True" />
<%--<asp:CheckBox ID="CheckBox1" runat="server" Commandname="RowClick" OnCheckedChanged="ToggleRowSelection"/>--%>
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="headerChkbox" runat="server" enabled="true" OnCheckedChanged="ToggleSelectedState"
AutoPostBack="True" />
</HeaderTemplate>
<ItemStyle Width="20px" />
<HeaderStyle Width="20px" />
</radG:GridTemplateColumn>

0
Angel Petrov
Telerik team
answered on 11 Feb 2016, 08:28 AM
Hi,

Changing the .NET version should not cause such behavior. Can you please send us a sample which illustrates the problem?

Additionally please try out the following.
  1. If the application uses AJAX please disable it and test whether a server-side exception is thrown.
  2. Verify that there are no scripts that have failed to load.

Regards,
Angel Petrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Jimmy Stuart
Top achievements
Rank 1
Answers by
plamen
Top achievements
Rank 1
Simon
Top achievements
Rank 2
jonnyO
Top achievements
Rank 1
Steve Newbery
Top achievements
Rank 1
Iana Tsolova
Telerik team
Sebastian
Telerik team
Carl
Top achievements
Rank 1
jimmie
Top achievements
Rank 1
Keith Heilveil
Top achievements
Rank 1
Shawn Krivjansky
Top achievements
Rank 1
Joshua Warren
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
sroussos
Top achievements
Rank 2
Angel Petrov
Telerik team
Sonny
Top achievements
Rank 1
Marin Bratanov
Telerik team
Neha
Top achievements
Rank 1
Share this question
or