Add rows dynamically in Radgrid.

3 posts, 0 answers
  1. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 29 Mar 2012 Link to this post

    Hi Team,
    I want to add rows dynamically in radgrid.
    The number and types of columns in my grid are fixed.
    All columns are template columns.

    This is what I have :
    <asp:TextBox ID="txtNoOfAssets" runat="server" />
    <asp:Button ID="btnAddAssets" runat="server" Text="Add Assets" OnClick="btnAddAssets_Click" />
     
    <telerik:RadGrid ID="rgAUC" runat="server">
                            <MasterTableView CommandItemDisplay="None">
                                <Columns>
                                    <telerik:GridTemplateColumn HeaderText="Asset Name" UniqueName="AssetName">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAssetName" runat="server" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Asset No" UniqueName="AssetNo">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAssetNo" runat="server" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Description" UniqueName="AssetDescription">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAssetDescription" runat="server" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Class" UniqueName="AssetClass">
                                        <ItemTemplate>
                                            <telerik:RadComboBox ID="rcbAssetClass" runat="server"></telerik:RadComboBox>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Useful Life" UniqueName="AssetUsefulLife">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAssetUsefulLife" runat="server" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Original Value" UniqueName="AssetOriginalValue">
                                        <ItemTemplate>
                                            <asp:TextBox ID="txtAssetOriginalValue" runat="server" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>


    Now, on click of a button ' btnAddAssets  ', i want to add those many rows which user has put in txtNoOfAsset.
    Number and types of my columns are fixed.

    Any help appreciated,
    Thanks,
    Lok..
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 29 Mar 2012 Link to this post

    Hi Lokesh,

    Please take a look into the following code snippet I have tried for adding rows on Button click according to the number give in the TextBox.

    ASPX:
    <telerik:RadGrid ID="rgAUC" runat="server" onneeddatasource="rgAUC_NeedDataSource">
        <MasterTableView CommandItemDisplay="None">
            <Columns>
                <telerik:GridTemplateColumn HeaderText="Asset Name" UniqueName="AssetName">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAssetName" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Asset No" UniqueName="AssetNo">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAssetNo" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Description" UniqueName="AssetDescription">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAssetDescription" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Class" UniqueName="AssetClass">
                    <ItemTemplate>
                        <telerik:RadComboBox ID="rcbAssetClass" runat="server"></telerik:RadComboBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Useful Life" UniqueName="AssetUsefulLife">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAssetUsefulLife" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Original Value" UniqueName="AssetOriginalValue">
                    <ItemTemplate>
                        <asp:TextBox ID="txtAssetOriginalValue" runat="server" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    int num = 0;
    protected void btnAddAssets_Click(object sender, EventArgs e)
    {
        num = Convert.ToInt32(txtNoOfAssets.Text);
        rgAUC.Rebind();
    }
     
    protected void rgAUC_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        DataTable ds = new DataTable();
        rgAUC.DataSource = addBlankLines(ds,num);
    }
    private DataTable addBlankLines(DataTable ds, int num)
    {
        int count = 0;
             
        if (num != 0)
        {
            DataRow drBlank = default(DataRow);
                 
            for (int i = 0; i <num; i++)
            {
                    
                drBlank = ds.NewRow();
                ds.Rows.Add(drBlank);
                    
                    
            }
        }
            return ds;
            
    }

    Hope this helps.

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 30 Mar 2012 Link to this post

    Hi Shinu,
    Thanks for your reply.
    Your solution worked but not completely.
    It adds rows one time only. If user changes the value in textbox, those number of rows dont get added in grid.
    I tried my own way and its working fine...

    Here is what I have done :

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack && ViewState["dt"] == null)
                {
                    dt = new DataTable();
                    dt.Columns.Add(new DataColumn("Text", typeof(string)));
                    dt.Columns.Add(new DataColumn("Text1", typeof(string)));
                    dt.Columns.Add(new DataColumn("Text2", typeof(string)));
                    dt.Columns.Add(new DataColumn("Selected", typeof(string)));
                    dt.Columns.Add(new DataColumn("Text3", typeof(string)));
                    dt.Columns.Add(new DataColumn("Text4", typeof(string)));
      
                    ViewState["dt"] = dt;
                }
                else
                {
                    dt = (DataTable)ViewState["dt"];
      
                    int RowCnt = dt.Rows.Count;
      
                    for (int i = RowCnt; i >= 0 && (i - 1) >= 0; i--)
                    {
                        dt.Rows.RemoveAt(i - 1);
                    }
      
                    dt = (DataTable)ViewState["dt"];
                
            }
      
    protected void btnAddAssets_Click(object sender, EventArgs e)
            {
                try
                {
                    DataTable dt = (DataTable)ViewState["dt"];
                    foreach (GridDataItem item in rgAUC.Items)
                    {
                        TextBox txtAssetName = (TextBox)item.FindControl("txtAssetName");
                        TextBox txtAssetNo = (TextBox)item.FindControl("txtAssetNo");
                        TextBox txtAssetDescription = (TextBox)item.FindControl("txtAssetDescription");
                        RadComboBox rcbAssetClass = (RadComboBox)item.FindControl("rcbAssetClass");
                        TextBox txtAssetUsefulLife = (TextBox)item.FindControl("txtAssetUsefulLife");
                        TextBox txtAssetOriginalValue = (TextBox)item.FindControl("txtAssetOriginalValue");
      
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (item.ItemIndex == i)
                            {
                                dt.Rows[i][0] = txtAssetName.Text;
                                dt.Rows[i][1] = txtAssetNo.Text;
                                dt.Rows[i][2] = txtAssetDescription.Text;
                                dt.Rows[i][3] = rcbAssetClass.SelectedValue;
                                dt.Rows[i][4] = txtAssetUsefulLife.Text;
                                dt.Rows[i][5] = txtAssetOriginalValue.Text;
                            }
                        }
                    }
      
                    dt = AddRow(dt);
                    rgAUC.Rebind(); 
                }
                catch (Exception ex)
                {
                    XITingExceptionProcessor.ProcessException(this, ex);
                }
            }
      
     protected void rgAUC_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                try
                {
                    ViewState["dt"] = dt;
                    dt = (DataTable)ViewState["dt"];
                    rgAUC.DataSource = dt;
                }
                catch (Exception ex)
                {
                    XITingExceptionProcessor.ProcessException(this, ex);
                }
            }
      
    private DataTable AddRow(DataTable dt)
            {
                  
      
                // method to create row  
                for (int i = 0; i < int.Parse(txtNoOfAssets.Text); i++)
                {
                    DataRow dr = dt.NewRow();
                    dr["Text"] = "";
                    dr["Text1"] = "";
                    dr["Text2"] = "";
                    dr["Selected"] = "";
                    dr["Text3"] = "";
                    dr["Text4"] = "";
                    dt.Rows.Add(dr);
                }
                return dt;
            }

    Thanks for your reply again.
    Hope my code also helps someone.

    Lok..
Back to Top