Convert GridCheckBoxColumn value to Boolean

5 posts, 0 answers
  1. Perry
    Perry avatar
    42 posts
    Member since:
    Jul 2015

    Posted 14 Sep Link to this post

    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?

  2. Loïc
    Loïc avatar
    27 posts
    Member since:
    Oct 2016

    Posted 15 Sep in reply to Perry Link to this post

    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.

  3. Perry
    Perry avatar
    42 posts
    Member since:
    Jul 2015

    Posted 15 Sep in reply to Loïc Link to this post

    I have to convert it because my database structure does not accept the text value
  4. Perry
    Perry avatar
    42 posts
    Member since:
    Jul 2015

    Posted 15 Sep Link to this post

    I was able to accomplish the goal by using this code

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

  5. Perry
    Perry avatar
    42 posts
    Member since:
    Jul 2015

    Posted 15 Sep Link to this post

    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

Back to Top