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

Add new row in Grid

3 Answers 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tarulatta Patel
Top achievements
Rank 1
Tarulatta Patel asked on 04 Feb 2009, 07:21 PM

Hi,

 

I have one Telerik grid and one button on my page. Clicking on button I want to add one row in grid and row will have combobox, textbox etc controls. Users can click multiple times on button and on every click I want to add extra row with controls in grid. And I also want to capture events of these dynamic loaded controls.

 

Please provide me the code to add new row in grid with other requirements.

 

Thanks.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Feb 2009, 12:00 PM
Hello Tarulatta,

You can create a new DataTable and add columns to it in the PageLoad event. Then in the NeedDataSource event of the grid add rows to the DataTable and save it in a session variable which should be bound to the grid. Then on button click you can add more rows to the DataTable and rebind the grid to the new DataTable. The following code should help you understand better :
aspx:
 <telerik:GridTemplateColumn UniqueName="TemplateColumn"
            <ItemTemplate> 
                <asp:DropDownList ID="DropDownList1" runat="server"
                <asp:ListItem Text="1"></asp:ListItem> 
                <asp:ListItem Text="2"></asp:ListItem> 
                <asp:ListItem Text="3"></asp:ListItem> 
                <asp:ListItem Text="4"></asp:ListItem> 
                    
                </asp:DropDownList> 
            </ItemTemplate> 
        </telerik:GridTemplateColumn> 

cs:
  public DataTable dt; 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            dtnew DataTable(); 
            dt.Columns.Add("Combo");     
 
        } 
 
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            if (!IsPostBack) 
            {               
                dt=  AddRow(dt);  // call the method to create row
                ViewState["dt"] = dt; 
            } 
            dt = (DataTable)ViewState["dt"]; 
            RadGrid1.DataSource = dt
        } 
 
        private DataTable AddRow(DataTable dt)
        { // method to create row
            DataRow dr = dt.NewRow(); 
            dr[0] = ""; 
           
            dt.Rows.Add(dr); 
            return dt; 
        } 
 
        protected void Button1_Click(object sender, EventArgs e) 
        { 
            DataTable dt = (DataTable)ViewState["dt"]; 
            ViewState["dt"] = AddRow(dt); 
            RadGrid1.Rebind(); 
                        
        } 

Thanks
Princy.
0
Tarulatta Patel
Top achievements
Rank 1
answered on 06 Feb 2009, 06:34 PM

I am able to add row in Datatable and that row I can see in the grid also. But It does not retain the values entered in the grid.

I would like to add row and from front-end whatever the value user gonna entered that has to remain. after adding second row..

Any time I add new row it lost the previous changes on the grid. Grid changes has to remains.

Is there any wat I can get back the Grid value back and store in datatable.

OR do u have client side add row feture 

do u have bydefulat inline edit grid row.

Please replay me asap.

Thanks,
Tarulatta Patel
0
Princy
Top achievements
Rank 2
answered on 11 Feb 2009, 08:08 AM
Hello Tarulatta,

You can try out the following code to set the Grid in InsertMode by default:
cs:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        RadGrid1.MasterTableView.IsItemInserted = true
        RadGrid1.MasterTableView.Rebind(); 
    } 
 

You can also refer to the following online demo which demonstrates on how to add insert fields on clicking a button. You can use insertForms and alter the logic in the demo to meet your requirements.
MultiInsert in RadGrid using RadToolTip

Thanks
Princy.
Tags
Grid
Asked by
Tarulatta Patel
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Tarulatta Patel
Top achievements
Rank 1
Share this question
or