How to Bind Checkbox in EditForm Template for RadGrid
Environment
Product Version | 2019.3.1023 |
Product | RadGrid for ASP.NET AJAX |
Description
Sometimes it is necessary to bind a Checkbox in a RadGrid Edit Form Template. This can be accomplished using the Bind method on the DataField.
Solution
Using a BIT Data Type in a Database as shown below, use the Bind method in ASP.NET Web Forms.
The Table Schema
CREATE TABLE dbo.IsTrue
(
Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
IsTrue BIT NOT NULL,
);
GO
The Grid Markup
<telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None"
AutoGenerateColumns="False"
ShowStatusBar="true"
AllowAutomaticDeletes="True"
AllowAutomaticInserts="True"
AllowAutomaticUpdates="True"
DataSourceID="SqlDataSource1">
<MasterTableView CommandItemDisplay="Top" DataSourceID="SqlDataSource1"
DataKeyNames="Id">
<Columns>
<telerik:GridEditCommandColumn>
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn UniqueName="Id" Display="true" HeaderText="ID" DataField="Id"></telerik:GridBoundColumn>
<telerik:GridCheckBoxColumn UniqueName="IsTrue" HeaderText="Is True" DataField="IsTrue">
</telerik:GridCheckBoxColumn>
<telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="column">
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<FormTemplate>
<asp:CheckBox ID="Checkbox1" runat="server" Checked='<%# Bind("IsTrue") %>' />
<%--or--%>
<%--<asp:CheckBox runat="server" ID="CheckBox1" Checked='<%#(Container is GridEditFormInsertItem) ? false : DataBinder.Eval(Container.DataItem,"IsTrue")%> '/>--%>
<asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
</FormTemplate>
</EditFormSettings>
</MasterTableView>
<ClientSettings>
<ClientEvents OnRowDblClick="RowDblClick"></ClientEvents>
</ClientSettings>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
InsertCommand="INSERT INTO IsTrue (IsTrue) VALUES(@IsTrue)"
SelectCommand="SELECT * FROM IsTrue"
UpdateCommand="UPDATE IsTrue SET IsTrue = @IsTrue WHERE Id = @Id"
DeleteCommand="DELETE FROM IsTrue WHEW Id = @Id">
<InsertParameters>
<asp:Parameter Name="IsTrue" Type="Boolean" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="IsTrue" Type="Boolean" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32"/>
</DeleteParameters>
</asp:SqlDataSource>