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

Convert GridCheckBoxColumn value to Boolean

4 Answers 53 Views
CheckBox
This is a migrated thread and some comments may be shown as answers.
Perry
Top achievements
Rank 1
Perry asked on 14 Sep 2017, 03:56 PM
I have this RadGrid
<telerik:RadGrid ID="gvMembers" runat="server"
            AutoGenerateColumns="false"
            OnNeedDataSource="gvMembers_NeedDataSource"
            AllowSorting="true"
            AllowPaging="true"
            PageSize="10"
            AllowAutomaticUpdates="true"
            AllowAutomaticInserts="true"
            AllowAutomaticDeletes="true"
            OnItemCreated="gvMembers_ItemCreated"
            OnItemInserted="gvMembers_ItemInserted"
            OnPreRender="gvMembers_PreRender"
            OnInsertCommand="gvMembers_InsertCommand"
            OnItemDataBound="gvMembers_ItemDataBound"
            OnUpdateCommand="gvMembers_UpdateCommand"
            OnDeleteCommand="gvMembers_DeleteCommand"
 
            >
            <HeaderStyle CssClass="GridHeader" />
            <PagerStyle Mode="NextPrevNumericAndAdvanced" />
            <MasterTableView AutoGenerateColumns="false" DataKeyNames="UserID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
                <Columns>
                    <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="UserID" HeaderText="UserID" ReadOnly="true" UniqueName="UserID"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" UniqueName="firstname"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="Last Name" SortExpression="LastName" UniqueName="lastname"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" SortExpression="UserName" UniqueName="username"></telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="EmailAddress" HeaderText="Email" SortExpression="EmailAddress" UniqueName="email"></telerik:GridBoundColumn>
 
                    <telerik:GridTemplateColumn UniqueName="TemplateColumn" HeaderText="Role">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server"
                                Text='<%# DataBinder.Eval(Container.DataItem, "Role") %>'>
                            </asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlRole" runat="server" DataTextField="Role1" DataValueField="RoleID"></asp:DropDownList>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridButtonColumn ConfirmText="Delete this Member?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="PushButton" Text="Delete" CommandName="Delete" />
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>

 

I am using a stored procedure to to my updates and have this C# code:

protected void gvMembers_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            var editableItem = ((GridEditableItem)e.Item);
            var memberId = (int)editableItem.GetDataKeyValue("UserID");
            int Role = 1;
            CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
            Boolean bactive = Convert.ToBoolean(active);
            string str = active.Text;
            string strFirstName = (editableItem["firstname"].Controls[0] as TextBox).Text;
            string strLastName = (editableItem["lastname"].Controls[0] as TextBox).Text;
            string strUserName = (editableItem["username"].Controls[0] as TextBox).Text;
            string strEmail = (editableItem["firstname"].Controls[0] as TextBox).Text;
 
 
 
            try
                {
                //save chanages to Db
DbContext.AAHomicideLogMembersUpdate(memberId, Role, strFirstName, strLastName, strUserName, strEmail, active);
                }
                catch (System.Exception)
                {
                   string srtt="Dosomething";
                }
            }

 

I get an error message when attempting to convert the checkbox value to a boolean value.

 

How can I accomplish this?

4 Answers, 1 is accepted

Sort by
0
Loïc
Top achievements
Rank 1
answered on 15 Sep 2017, 01:07 PM

Hi Perry,

Why are you trying to convert a CheckBox to a boolean ? You could just try :

Boolean bactive = Convert.ToBoolean(active.Checked);

 

More information about Convert.ToBoolean here.

0
Perry
Top achievements
Rank 1
answered on 15 Sep 2017, 03:43 PM
I have to convert it because my database structure does not accept the text value
0
Perry
Top achievements
Rank 1
answered on 15 Sep 2017, 03:45 PM

I was able to accomplish the goal by using this code

CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
               Boolean bactive = active.Checked

0
Perry
Top achievements
Rank 1
answered on 15 Sep 2017, 03:46 PM

I was able to make the conversion using this code

CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
               Boolean bactive = active.Checked;

Thanks for your response

Tags
CheckBox
Asked by
Perry
Top achievements
Rank 1
Answers by
Loïc
Top achievements
Rank 1
Perry
Top achievements
Rank 1
Share this question
or