How to add rows dynamically to radgird

13 posts, 0 answers
  1. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 09 Jan 2012 Link to this post

    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.
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 09 Jan 2012 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 09 Jan 2012 Link to this post

    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.
  5. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 09 Jan 2012 Link to this post

    How to add new blank row manually. i am newer to radgrid
  6. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 10 Jan 2012 Link to this post

    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.
  7. Dhaval
    Dhaval avatar
    41 posts
    Member since:
    Aug 2010

    Posted 10 Jan 2012 Link to this post

    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
  8. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 10 Jan 2012 Link to this post

    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
  9. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 10 Jan 2012 Link to this post

    Thank You So Much Jayesh. this is the one exact code i want
  10. Yogish
    Yogish avatar
    12 posts
    Member since:
    Dec 2010

    Posted 11 Jan 2012 Link to this post

    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
  11. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 11 Jan 2012 Link to this post

    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
  12. Keith
    Keith avatar
    5 posts
    Member since:
    Mar 2012

    Posted 24 May 2012 Link to this post

    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.
  13. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 29 May 2012 Link to this post

    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.
  14. Shridhar
    Shridhar avatar
    1 posts
    Member since:
    May 2014

    Posted 22 May 2014 in reply to Jayesh Goyani Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017