This is a migrated thread and some comments may be shown as answers.
How to add rows dynamically to radgird
12 Answers 411 Views
This is a migrated thread and some comments may be shown as answers.
Yogish
Top achievements
Rank 1
Yogish asked on 09 Jan 2012, 07:03 AM
I want to add rows to radgrid when i press add new button. it works first time fine. that mean it adds first row  properly when i press once agin it wont add new rows to grid.

12 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jan 2012, 07:27 AM
Hello Yogish,

for multiple inserting rows (Blank row for insert new item)  you have to do  this thing manually.

let me know if you have any concern.

Thanks,
Jayesh Goyani
0
Yogish
Top achievements
Rank 1
answered on 09 Jan 2012, 11:02 AM
Thank You for replaying.
How to add blank rows to rad grid.. in radgrid i   enabled addnew button. and when i press that  one empty row will created. after enter that i wan one more row for that what i  have to do.. please briefly explan me.
0
Yogish
Top achievements
Rank 1
answered on 09 Jan 2012, 11:34 AM
How to add new blank row manually. i am newer to radgrid
0
Yogish
Top achievements
Rank 1
answered on 10 Jan 2012, 09:38 AM
were you went man. i am not satisfying with your answer. i think you simply wast my time. again and again i am refreshing this page.
0
Dhaval
Top achievements
Rank 2
answered on 10 Jan 2012, 09:44 AM
Hi Yogesh,

You can use editformtemplate in which you can keep on html button and on click of that button you can append one tr to your current table..

Regards,
Dhaval Shukla
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Jan 2012, 11:47 AM
Hi Yogish ,

Sorry, for late reply.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
            onneeddatasource="RadGrid1_NeedDataSource"
            onitemdatabound="RadGrid1_ItemDataBound"
            onitemcommand="RadGrid1_ItemCommand">
        <MasterTableView DataKeyNames="IsAdd,ID" CommandItemDisplay="Top">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="ID" DataField="ID">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                    <asp:TextBox  ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:Button ID="Button1" runat="server" Text="Save" CommandName="MySave" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
 
        </Columns>
        </MasterTableView>
       </telerik:RadGrid>
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           DataTable dt = new DataTable();
           dt.Columns.Add("ID");
           dt.Columns.Add("IsAdd");
           dt.Rows.Add(1, false);
           dt.Rows.Add(2, false);
           dt.Rows.Add(3, false);
           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;
           Label Label1 = item.FindControl("Label1") as Label;
           TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
           Button Button1 = item.FindControl("Button1") as Button;
 
 
 
           bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
           if (isAdd)
           {
               Label1.Visible = false;
           }
           else
           {
               TextBox1.Visible = false;
               Button1.Visible = false;
           }
            
       }
   }
   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();
       }
       if (e.CommandName == "MySave")
       {
           GridDataItem item = e.Item as GridDataItem;
           TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
 
           DataTable dt = (DataTable)Session["dt"];
           dt.Rows.Add(TextBox1.Text, false);
 
           foreach (DataRow dr in dt.Rows)
           {
               if (Convert.ToBoolean(dr["IsAdd"].ToString()))
               {
                   dt.Rows.Remove(dr);
                   break;
               }
           }
 
           RadGrid1.Rebind();
       }
   }


Let me know if any concern.

Thanks,
Jayesh Goyani
0
Yogish
Top achievements
Rank 1
answered on 11 Jan 2012, 05:11 AM
Thank You So Much Jayesh. this is the one exact code i want
0
Yogish
Top achievements
Rank 1
answered on 11 Jan 2012, 06:49 AM
Hi Jayesh..

I am go through your last post and also able to insert rows. in Your example you used only one column but in real world more than one column is required right .  i add exatra columum to ur  desgin. "

 

 

bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));

 

 for above line it gives error like String was not recognized as a valid Boolean. please send me thproper code to insert more than one columns. i add one screen shot like that way i want. 
best regards 
          yogish m shetty
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Jan 2012, 09:10 AM
Hello Yogish,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
            OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand">
            <MasterTableView DataKeyNames="IsAdd,ID" CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="ID" DataField="ID">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn UniqueName="Name" DataField="Name">
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                            <asp:Button ID="Button1" runat="server" Text="Save" CommandName="MySave" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("ID");
          dt.Columns.Add("Name");
          dt.Columns.Add("IsAdd");
           
          dt.Rows.Add(1,"Name1", false);
          dt.Rows.Add(2,"Naem2", false);
          dt.Rows.Add(3,"Name3", false);
          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;
          Label Label1 = item.FindControl("Label1") as Label;
          TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
          Button Button1 = item.FindControl("Button1") as Button;
 
          Label Label2 = item.FindControl("Label2") as Label;
          TextBox TextBox2 = item.FindControl("TextBox2") as TextBox;
 
 
 
          bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
          if (isAdd)
          {
              Label1.Visible = Label2.Visible = false;
          }
          else
          {
              TextBox1.Visible = TextBox2.Visible = false;
              Button1.Visible = false;
          }
 
      }
  }
  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;
          RadGrid1.Rebind();
      }
      if (e.CommandName == "MySave")
      {
          GridDataItem item = e.Item as GridDataItem;
          TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
          TextBox TextBox2 = item.FindControl("TextBox2") as TextBox;
 
          DataTable dt = (DataTable)Session["dt"];
          dt.Rows.Add(TextBox1.Text,TextBox2.Text, false);
 
          foreach (DataRow dr in dt.Rows)
          {
              if (Convert.ToBoolean(dr["IsAdd"].ToString()))
              {
                  dt.Rows.Remove(dr);
                  break;
              }
          }
 
          RadGrid1.Rebind();
      }
  }


Thanks,
Jayesh Goyani
0
Keith
Top achievements
Rank 1
answered on 24 May 2012, 08:38 AM
Hi Jayesh,

I have implemented the code you provided below and it works fine on runtime; however, when I try to insert the data to our SQL database it fails to insert the data I provided in the text boxes (saved in the grid). So my question is how do I insert the data from those dynamic textboxes to the database after clicking the save button (see screen shot attached).
I have put this grid in an existing form and I want to insert the form data (text boxes) and the grid data to the database. What happens now is that only the form data inserts to the database and not the grid data.

I have attached an example of what I want to achieve. The "blue" save button should commit the data in the grid to the database.

Please can you assist me to achieve this insert.

Regards,
Edward.
0
Andrey
Telerik team
answered on 29 May 2012, 08:17 AM
Hi,

In order to Insert/Update the data from RadGrid in the database RadGrid should be in Insert/Edit mode. This is necessary because the column Editors should be initialized before you take the cell value. Once you get the cell value you could easily insert/update it to the database.

RadGrid could be entered into Insert/Edit mode by the autogenerated AddNewRecord/Edit buttons of RadGrid. Then you could handle the Insert/Update command events.

Kind regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Shri
Top achievements
Rank 1
answered on 22 May 2014, 04:42 PM
Hi Jayesh, thanks for the  function but can you also please provide me how to delete the records by adding 'Delete' button? The save functionality should save in the database based on final number of records available on the radgrid.

Appreciate your help.
Tags
Grid
Asked by
Yogish
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Yogish
Top achievements
Rank 1
Dhaval
Top achievements
Rank 2
Keith
Top achievements
Rank 1
Andrey
Telerik team
Shri
Top achievements
Rank 1
Share this question
or