Add Multiple Rows in Telerik Radgrid on Button click and Save its Data

Add Multiple Rows in Telerik Radgrid on Button click and Save its Data

Step 1: Design web form .aspx page

  <div>
        <asp:ScriptManager ID="SM" runat="server">
        </asp:ScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" Width="50%"
            OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound"
            OnItemCommand="RadGrid1_ItemCommand">
            <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="sa">
                        <ItemTemplate>
                            <asp:TextBox ID="TextBox" runat="server" Text='<%# Eval("ID") %>'></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn UniqueName="sa">
                        <ItemTemplate>
                            <asp:DropDownList ID="drd" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <asp:Button ID="Button1" runat="server" Text="Add new row" OnClick="Button1_Click" />
        <br />
        <br />
        <asp:Button ID="save" Text="Save" runat="server" OnClick="save_Click" />
    </div>



Step 2: Logic on .CS page

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            Session["dt"] = dt;
        }
    }




    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid1.DataSource = (DataTable)Session["dt"];
    }
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            DropDownList drd = item.FindControl("drd") as DropDownList;

            DataTable DT1 = new DataTable();
            DT1.Columns.Add("ID", typeof(int));
            DT1.Columns.Add("name");

            DT1.Rows.Add(1, "sam");
            DT1.Rows.Add(2, "kandY");
            drd.DataTextField = "name";
            drd.DataValueField = "ID";
            drd.DataSource = DT1;
            drd.DataBind();
        
        }
    }
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            DataTable dt = (DataTable)Session["dt"];
            dt.Rows.Add(0, true);
            RadGrid1.MasterTableView.IsItemInserted = false;
            e.Canceled = true;
            RadGrid1.Rebind();
        }
      
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        string strtxt = string.Empty;
        string txt = string.Empty;
        foreach (GridDataItem di in RadGrid1.Items)
        {
            TextBox textBox1 = (TextBox)(di.FindControl("TextBox"));
            DropDownList drd = (DropDownList)(di.FindControl("drd"));
            dt.Rows.Add(textBox1.Text, drd.SelectedItem.Text);
        }
        Session["dt"] = dt;
        GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];
        commandItem.FireCommandEvent("InitInsert", null);

    }
    protected void save_Click(object sender, EventArgs e)
    {
        string strtxt = string.Empty;
        string txt = string.Empty;
        foreach (GridDataItem di in RadGrid1.Items)
        {
    TextBox textBox1 = (TextBox)(di.FindControl("TextBox"));
            DropDownList drd = (DropDownList)(di.FindControl("drd"));

            txt += textBox1.Text + ",";
            strtxt += drd.SelectedItem.Text + ",";
        }
        Response.Write("Column Value : " + strtxt);
        Response.Write("<br/>");
        Response.Write("Control Value : " + txt);
    }


It will look like :-




Comments

  1. Hi
    I am trying to duplicate the above example and I am getting this error in lines. Am I missing something?







    Telerik.Web.UI.GridColumnCollection must have items of type 'Telerik.Web.UI.GridColumn'. 'asp:Button' is of type 'System.Web.UI.WebControls.Button'.

    ReplyDelete
  2. Continuatoin of the previous message. getting error in last four lines before closing div tag in .aspx page

    ReplyDelete
  3. How to bind timepicker to the newly added textboxes?

    ReplyDelete
    Replies
    1. Pleas refer this ,
      instead of binding the Time picker to a textbox ,you can use its control directly.

      http://kandydeol.blogspot.in/2013/09/telerik-datetime-and-datetime-controls.html

      Delete
  4. Thanks and that i have a tremendous offer you: Where To Start With Whole House Renovation whole home renovation

    ReplyDelete

Post a Comment

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click