In this article, We have explained regarding Dynamically Bind RadGrid in ASP.NET using DataTable. RadGrid is a Telerik control. These are the following Codes...
Write this code in Design Page:
<telerik:RadGrid ID="RadGrid1" runat="server" Width="70%" ShowFooter="True" MasterTableView-NoDetailRecordsText="" MasterTableView-NoMasterRecordsText="" AutoGenerateColumns="false" ShowHeader="False" OnItemCommand="RadGrid1_ItemCommand">
<MasterTableView NoDetailRecordsText="" NoMasterRecordsText="" > <Columns> <telerik:GridTemplateColumn Display="false"> <ItemTemplate> <telerik:RadLabel ID="lblid" runat="server" Text='<%# Bind("SkillLevel_Id") %>'></telerik:RadLabel> </ItemTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn> <ItemTemplate> <telerik:RadLabel ID="lbllevel1" runat="server" Text='<%# Bind("Skill_LevelName") %>'></telerik:RadLabel> </ItemTemplate> <FooterTemplate> <telerik:RadTextBox ID="txtlevel2" runat="server" MaxLength="10"></telerik:RadTextBox> </FooterTemplate> </telerik:GridTemplateColumn> <telerik:GridTemplateColumn> <ItemTemplate> <telerik:RadButton ID="btnclose" runat="server" CommandName="cancel" Text="X" CausesValidation="false"></telerik:RadButton> </ItemTemplate> <FooterTemplate> <telerik:RadButton ID="btnadd" CommandName="add_level" runat="server" Text="+" CausesValidation="false" ValidationGroup="level"></telerik:RadButton> </FooterTemplate> </telerik:GridTemplateColumn> </Columns> </MasterTableView> </telerik:RadGrid>
Write this Below code in Code-Behind Page:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { temptable(); } } private void temptable() { DataTable dt = new DataTable(); dt.Columns.Add("SkillLevel_Id", typeof(string)); dt.Columns.Add("Skill_LevelName", typeof(string)); Session["dt"] = dt; BindGrid(); } protected void BindGrid() { RadGrid1.DataSource = Session["dt"] as DataTable; RadGrid1.DataBind(); }
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e) { if (e.CommandName == "add_level") { GridFooterItem footerItem = (GridFooterItem)e.Item; if (((RadTextBox)footerItem.FindControl("txtlevel2")).Text != "") { DataTable dt = (Session["dt"]) as DataTable; DataRow dr = dt.NewRow(); dr["SkillLevel_Id"] = ""; dr["Skill_LevelName"] = ((RadTextBox)footerItem.FindControl("txtlevel2")).Text; dt.Rows.Add(dr); Session["dt"] = dt; BindGrid(); } else { } } if (e.CommandName == "cancel") { ViewState["dt"]=Session["dt"]; DataTable dt = (ViewState["dt"]) as DataTable; int i = e.Item.ItemIndex; dt.Rows.RemoveAt(i); Session["dt"] = dt; BindGrid(); }
}
0 Comments