On adding new rows to the radgrid losing the data

2 posts, 0 answers
  1. Srividhya
    Srividhya avatar
    2 posts
    Member since:
    Jun 2016

    Posted 15 Sep Link to this post

    My requirement is on pageload since I've nothing to bind to radgrid I need to just show the grid headers with "Add new row" button".
    1.  When the user click the "Add new row' button a new row is created with 2 textboxes and 2 dropdown(binds data from the database) to the radgrid row. 
    2. Each row should have remove row button
    3. at the radgrid footer I've the Button "save to db"
    4. after adding the  data to the new row(not saved yet to the db) i need to add more rows. When I click on "Add new row' button I lose all the data entered. Technically saying on postback losing all data. this part is where I need help on how to handle this. 

    Finally after adding all rows I save the data to database.

    *************Thoughout this functionality I have no data to rebind the radgrid from the database**************************

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" MasterTableView-CommandItemSettings-ShowAddNewRecordButton="false"  MasterTableView-CommandItemSettings-ShowRefreshButton="false" OnNeedDataSource="RadGrid1_NeedDataSource"
                OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand">
                <MasterTableView Width="100%" HeaderStyle-Font-Bold="true" CommandItemStyle-Font-Bold="true" DataKeyNames="IsAdd,CourseID"  CommandItemDisplay="Top" CommandItemStyle-HorizontalAlign="Right">
                        <asp:Button ID="IsAdd" Font-Size="Small" Font-Bold="true" CommandName="InitInsert" Text ="Add Course" runat="server" />
                        <telerik:GridTemplateColumn UniqueName="CourseID" HeaderText="Course #" DataField="CourseID">
                               <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        <telerik:GridTemplateColumn UniqueName="CourseDept"  HeaderText="Course dept" DataField="DeptID">
                              <asp:DropDownList ID="DeptDropDown" DataField="DeptID" DataTextField="DeptName" runat="server" AutoPostBack="false"></asp:DropDownList>
                        <telerik:GridTemplateColumn UniqueName="LocationName" HeaderText="Course Location" DataField="LocationID">
                               <asp:DropDownList ID="locationDropDown" DataTextField="locationName" runat="server" AutoPostBack="false"></asp:DropDownList>              
                        <telerik:GridTemplateColumn UniqueName="Name" HeaderText="Remarks">
                                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                <asp:Button ID="Button1" runat="server" Text="Remove Row" CommandName="Delete" />
            <asp:Button ID="savebtn" runat="server" Font-Bold="true" Text="Save the course"/>
    protected void Page_Load(object sender, EventArgs e)
                DataTable dt = new DataTable();        
                if (!IsPostBack)
                    Session["dt"] = dt;
            protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
                RadGrid1.DataSource = (DataTable)Session["dt"];
            protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
                Button btn = new Button();         
                if (e.Item is GridDataItem)
                    GridDataItem item = e.Item as GridDataItem;
                    TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
                    Button Button1 = item.FindControl("Button1") as Button;               
    (I've deptdropdown and locationdropdown binded here)
                    TextBox TextBox4 = item.FindControl("TextBox4") as TextBox;
                    bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
                    if (isAdd)
                        TextBox1.Visible = LocationDropDown.Visible = DeptDropDown.Visible = TextBox4.Visible = true;
                        btn.Visible = true;
                        RadGrid1.DataSource = Session["dt"];
            protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
                if (e.CommandName == RadGrid.InitInsertCommandName)
                    DataTable dt = (DataTable)Session["dt"];
                    dt.Rows.Add(0, string.Empty, true);
                    RadGrid1.MasterTableView.IsItemInserted = false;
                    e.Canceled = true;

    KIndly help me solve this. Provided the code below.

  2. Kostadin
    Kostadin avatar
    1687 posts

    Posted 20 Sep Link to this post

    Hello Srividhya,

    I would recommend you to use batch editing to achieve your requirement. You can check out the edit mode in action in the following live example. Also you can examine the documentation for more information.

    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
  3. DevCraft R3 2016 release webinar banner
Back to Top