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

insert rows using javascript

3 Answers 173 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sandeep Mangal
Top achievements
Rank 1
Sandeep Mangal asked on 16 Mar 2010, 10:21 AM
Is there any way to insert rows into a radgrid via javascript, without using the edit form?
Suppose I have a radgrid with some rows of data into it. Now I have a button and I want that on click of that button a new blank row should be inserted in the grid where user can enter their data and later it can be saved with some another button.
Is there any way to add a blank row using javascript.
Please suggest.
Thanks.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Mar 2010, 11:17 AM
Hi.

You can make use of the InsertItem method which inserts new table row to the grid. The new data will be taken from the insertion form editors fields. 

JS:

function AddNewItem() 
  var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView(); 
  masterTable.insertItem(); 
 
  

Princy
0
Sandeep Mangal
Top achievements
Rank 1
answered on 17 Mar 2010, 08:13 AM
Hi,
Thanks for the reply but I am still not able to add a row using this code. I don't know if I am missing any basic property or something in the grid but the code executes fine but the new row is not added to the grid.
Aspx Code:
<telerik:RadGrid ID="RadGrid1" AllowAutomaticInserts="true" AutoGenerateEditColumn="true" runat="server" > 
            <MasterTableView DataKeyNames="ID_APPLIESTO"  AllowAutomaticInserts="true" EditMode="InPlace" InsertItemDisplay="Bottom" AutoGenerateColumns="false" > 
                <Columns> 
                    <telerik:GridBoundColumn  DataField="ID_APPLIESTO"  DataType="System.Int32" HeaderText="ID_APPLIESTO"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn DataField="APPLIESTO_VALUE" DataType="System.Int32" HeaderText="Applies To"
                    </telerik:GridBoundColumn> 
                </Columns> 
        </MasterTableView> 
</telerik:RadGrid> 
<asp:Button ID="btnNew" runat="server" Text="Allocate" OnClientClick="Allocate()"  /> 
<script type="text/javascript"
   function Allocate() { 
      var tableView = $find("<%= RadGrid1.ClientID %>").get_masterTableView(); 
      tableView.insertItem(); 
   } 
</script> 

I have been populating my radgrid from code behind using a datatable in the Page_Load function:
RadGrid1.MasterTableView.DataSource = dt;

Can you please tell what am I missing here or what's wrong as code executes fine and loads data and on button click it goes to the javascript function and executes that code as well but it does not add any row to the grid.

Thanks.
0
Tsvetoslav
Telerik team
answered on 19 Mar 2010, 11:45 AM
Hello Sandeep,

As RadGrid is databound control, this scenario is not possible.

All the best,
Tsvetoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Sandeep Mangal
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Sandeep Mangal
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or