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

Validate 2 checkboxes in hierarchical detail grid

9 Answers 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
illumination
Top achievements
Rank 2
illumination asked on 16 Feb 2011, 05:39 PM
I have a hierarchy grid with master and detail grid. I need to be able to insert and update the detail grid. There are 2 checkboxes that if one is checked then the other one should not be checked. How do I validate or make this happen? Please help.
Thanks!

9 Answers, 1 is accepted

Sort by
0
illumination
Top achievements
Rank 2
answered on 16 Feb 2011, 09:17 PM
oncheckedchanged event, I kept having this error (please see image).
Please help me since I have tried to find all the right answer in forum and kb and still is not successful.
Thanks.
0
Princy
Top achievements
Rank 1
answered on 17 Feb 2011, 05:38 AM
Hello,

If you want only one CheckBox should be selected at a time, you can disable the other CheckBox like below.

ASPX:
<DetailTables>
    <telerik:GridTableView>
       <Columns>
           <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox2" runat="server" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
     </telerik:GridTableView>
</DetailTables>

C#:
Protected Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
    Dim chkbox1 As CheckBox = DirectCast(sender, CheckBox)
    Dim item As GridDataItem = DirectCast(chkbox1.NamingContainer, GridDataItem)
    Dim chkbox2 As CheckBox = DirectCast(item.FindControl("CheckBox2"), CheckBox)
    chkbox2.Enabled = Not chkbox1.Checked
End Sub

Similarly apply the same logic to CheckedChanged event of second CheckBox.

Thanks,
Princy.
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 02:33 PM
Thank you for the reply Princy.
How about if it is an edititemtemplate and not fro the itemtemplate in this detailtable? Because I need the insert and update function on the form template.
<telerik:GridTemplateColumn HeaderText="Encumbrance" SortExpression="Encumbrance" UniqueName="Encumbrance" DataField="Encumbrance" >
    <ItemTemplate>
        <asp:CheckBox ID="cbxEncumbrance" runat="server" Checked='<%# System.Convert.ToBoolean(Eval("Encumbrance").ToString()) %>'/>
    </ItemTemplate>
    <EditItemTemplate>
        <span><asp:CheckBox ID="cbxEncumbranceEdit" runat="server" Text="Encumbrance" 
        TextAlign="Right" AutoPostBack="True" Checked='<%# System.Convert.ToBoolean(Eval("Encumbrance").ToString()) %>' /></span>
    </EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Expenditures" SortExpression="Expenditures" UniqueName="Expenditures" DataField="Expenditures" >
    <ItemTemplate>
        <asp:CheckBox ID="cbxExpenditures" runat="server" Checked='<%# System.Convert.ToBoolean(Eval("Expenditures").ToString()) %>'/>
    </ItemTemplate>
    <EditItemTemplate>
        <span><asp:CheckBox ID="cbxExpendituresEdit" runat="server" Text="Expenditures" 
        TextAlign="Right" AutoPostBack="True" Checked='<%# System.Convert.ToBoolean(Eval("Expenditures").ToString()) %>'/></span>
    </EditItemTemplate>
</telerik:GridTemplateColumn>


Thanks again.
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 02:59 PM
I got this error when using this code.
Unable to cast object of type 'Telerik.Web.UI.GridEditFormItem' to type 'Telerik.Web.UI.GridDataItem'

on Dim

 

 

item As GridDataItem = DirectCast(cbxExpendituresEdit.NamingContainer, GridDataItem)

 

0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 03:20 PM
I changed to this code:
Protected Sub cbxEncumbranceEdit_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim cbxEncumbranceEdit As CheckBox = DirectCast(sender, CheckBox)
    Dim item As GridEditFormItem = DirectCast(cbxEncumbranceEdit.NamingContainer, GridEditFormItem)
    Dim cbxExpendituresEdit As CheckBox = DirectCast(item.FindControl("cbxExpendituresEdit"), CheckBox)
    'cbxExpendituresEdit.Enabled = Not cbxEncumbranceEdit.Checked
    cbxExpendituresEdit.Checked = True
    cbxEncumbranceEdit.Checked = False
End Sub
And it works, the problem I'm having now is when I click the insert or update button I got this error:
e.Exception = {"Cannot insert the value NULL into column 'Encumbrance', table 'PurchaseOrder'; column does not allow nulls. UPDATE fails. The statement has been terminated."}
What should I do?
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 03:48 PM
I saw another posting that you answered to about similar problem so this is what I did based on your instruction which I put the code under: Protected Sub RadGridFunding_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGridFunding.ItemDataBound
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode AndAlso Not e.Item.OwnerTableView.IsItemInserted Then
    'item about to edit
    Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
    Dim item1 As GridDataItem = DirectCast(editItem.ParentItem, GridDataItem)
    Dim chk1 As CheckBox = DirectCast(editItem.FindControl("cbxEncumbranceEdit"), CheckBox)
    Dim parentchk1 As CheckBox = DirectCast(item1.FindControl("cbxEncumbranceEdit"), CheckBox)
    Dim item2 As GridDataItem = DirectCast(editItem.ParentItem, GridDataItem)
    Dim chk2 As CheckBox = DirectCast(editItem.FindControl("cbxExpendituresEdit"), CheckBox)
    Dim parentchk2 As CheckBox = DirectCast(item2.FindControl("cbxExpendituresEdit"), CheckBox)
    If parentchk1.Checked Then
        chk1.Checked = parentchk1.Checked
        chk1.Checked = True
        chk2.Checked = False
    ElseIf parentchk2.Checked Then
        chk2.Checked = parentchk2.Checked
        chk1.Checked = False
        chk2.Checked = True
    End If
End If
But I got error (please see image).
Thank you.
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 04:13 PM
Progress. I changed the code to this
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode AndAlso Not e.Item.OwnerTableView.IsItemInserted Then
    'item about to edit
    Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
    Dim chk1 As CheckBox = DirectCast(editItem.FindControl("cbxEncumbranceEdit"), CheckBox)
    Dim chk2 As CheckBox = DirectCast(editItem.FindControl("cbxExpendituresEdit"), CheckBox)
    If chk1.Checked Then
        chk1.Checked = True
        chk2.Checked = False
    ElseIf chk2.Checked Then
        chk1.Checked = False
        chk2.Checked = True
    End If
End If
and I have no "object error" anymore but I still can't update.
my error still: e.Exception = {"Cannot insert the value NULL into column ...."}
Please help.
Thanks.
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 06:15 PM
Dear Princy,
To continue our conversation, please check my support ticket number 395198.
I posted a video file so you will be able to see the problem i'm having.
Thank you.
0
illumination
Top achievements
Rank 2
answered on 17 Feb 2011, 09:33 PM
I have solved this problem by assigning value in initinsert and checkbox_checkchanged.
Thanks.
Tags
Grid
Asked by
illumination
Top achievements
Rank 2
Answers by
illumination
Top achievements
Rank 2
Princy
Top achievements
Rank 1
Share this question
or