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

How to Add & Delete Dynamic rows in RadGrid

8 Answers 246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
salvio
Top achievements
Rank 1
salvio asked on 15 Nov 2010, 02:31 PM
Hai All,

How can i add rows dynamically in RadGrid
and can also delete any row in that grid not in database..
Pls...

8 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 16 Nov 2010, 07:47 AM
Hello Salvio,


If you want to add/delete rows in grid, but not in/from database, then a better approach is adding/deleting from the underlying datasource. Add the new row in the DataTable and rebind the grid and for deleting row, delete the row from DataTable which is set as DataSource for RadGrid.



-Shinu.
0
salvio
Top achievements
Rank 1
answered on 16 Nov 2010, 08:22 AM
Thanks for ur reply...

To delete a particular row i'm using the below code, but it's not deleting the specified row can u pls check
what code to add or remove...

 
Protected Sub rowrem_IT_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lb As LinkButton = DirectCast(sender, LinkButton)
        Dim gvRow As GridDataItem = DirectCast(lb.NamingContainer, GridDataItem)
        Dim rowID As Integer = gvRow.ItemIndex + 1
        If ViewState("IALV") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(ViewState("IALV"), DataTable)
            If dt.Rows.Count > 1 Then
                If gvRow.ItemIndex < dt.Rows.Count - 1 Then
                   
                    dt.Rows.Remove(dt.Rows(rowID))
                End If
            End If
           
            ViewState("IALV") = dt
           
            RadGrid1.DataSource = dt
            RadGrid1.DataBind()
        End If
 
    
        SetPreviousDatagrid()
    End Sub

Private Sub SetPreviousDatagrid()
        Dim rowIndex As Integer = 0
        If ViewState("IALV") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(ViewState("IALV"), DataTable)
            If dt.Rows.Count > 0 Then
                For i As Integer = 0 To dt.Rows.Count - 1
 
                    Dim box1 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrno_IT"), TextBox)
                    Dim box2 As RadComboBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("isocd_IT"), RadComboBox)
                    Dim box3 As RadComboBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrsize_IT"), RadComboBox)
                    Dim box4 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrheight_IT"), TextBox)
                    Dim box5 As RadComboBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrtype_IT"), RadComboBox)
                    
                    box1.Text = dt.Rows(i)("d1").ToString()
                    box2.Text = dt.Rows(i)("d2").ToString()
                    box3.Text = dt.Rows(i)("d3").ToString()
                    box4.Text = dt.Rows(i)("d4").ToString()
                    box5.Text = dt.Rows(i)("d5").ToString()
                   
                    rowIndex += 1
                Next
            End If
        End If
    End Sub


 
0
Iana Tsolova
Telerik team
answered on 18 Nov 2010, 02:17 PM
Hello salvio,

Your code seems correct. Can you specify if you have ajax on the page? If so, please share the ajax settings.

Best wishes,
Iana
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
salvio
Top achievements
Rank 1
answered on 19 Nov 2010, 08:16 AM
Thanks Lana for Your reply,

Below i sent my Code, the code contain Radgrid with dynamic ADD and Delete rows.
Add row is working, but I not know how to delete the particular row...If i click on remove btn my code is not deleting the specified row..
Pls check my code and reply how to do..
ASPX:
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
 
       <div>
    <telerik:radscriptmanager ID="RadScriptManager1" runat="server">
        </telerik:radscriptmanager>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
                                    ShowFooter ="True" GridLines="None" Height="227px" Skin="Web20"
                                    AllowPaging="True" AllowSorting="True" ShowGroupPanel="True">
                                 
                                <MasterTableView TableLayout="Fixed" >
                                 
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
                                 
                                <Columns >
                                 
                                 
                                 
                                 
                                 
                                 
                                 
                            
                                <telerik:GridTemplateColumn HeaderText ="Container Number" HeaderStyle-Width ="100px" >
                               <ItemTemplate >
                       <asp:TextBox ID="ctrno_IT" CssClass ="subfont" Width ="90px" Text='<%# Bind("d1") %>' runat="server"></asp:TextBox>
                       </ItemTemplate>
                                <FooterTemplate >
                                    <asp:ImageButton ID="ImageButton1" runat="server"
                                        ImageUrl ="~/IMAGES/Addrow.png" onclick="ImageButton1_Click" />
                                </FooterTemplate>
                                 
<HeaderStyle Width="100px"></HeaderStyle>
                                 
                                </telerik:GridTemplateColumn>
                                 
                                     <telerik:GridTemplateColumn HeaderText ="ISO Code"  HeaderStyle-Width ="70px" >
                               <ItemTemplate >
                                
                       <asp:TextBox ID="Isocd_IT" CssClass ="subfont" Width ="90px" Text='<%# Bind("d2") %>' runat="server"></asp:TextBox>
                                 
                       </ItemTemplate>
                               
                                 
<HeaderStyle Width="70px"></HeaderStyle>
                               
                                 
                                </telerik:GridTemplateColumn>
                                 
                                 
                                <telerik:GridTemplateColumn HeaderText =" Container Size" HeaderStyle-Width ="50px"  >
                               <ItemTemplate >
                       <asp:TextBox ID="Ctrsize_IT" CssClass ="subfont" Width ="90px" Text='<%# Bind("d3") %>' runat="server"></asp:TextBox>
                                
                       </ItemTemplate>
                               
                                 
<HeaderStyle Width="50px"></HeaderStyle>
                               
                                 
                                </telerik:GridTemplateColumn>
                                 
                                   
                                <telerik:GridTemplateColumn HeaderText ="Remove" HeaderStyle-Width ="100px"  >
                                <ItemTemplate
                   <asp:LinkButton ID="rowrem_IT" runat="server" onclick="rowrem_IT_Click">Remove</asp:LinkButton
                 </ItemTemplate>
                                 
<HeaderStyle Width="100px"></HeaderStyle>
                                 
                                </telerik:GridTemplateColumn>
                                 
                                 
                                </Columns>
                                </MasterTableView>
 
                                    <ClientSettings AllowColumnsReorder="True" AllowDragToGroup="True"
                                        ReorderColumnsOnClient="True">
                                        <Selecting AllowRowSelect="True" />
                                        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                                    </ClientSettings>
 
<FilterMenu EnableTheming="True" Skin="Default2006">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
                                </telerik:RadGrid>
    </div>
    </form>
</body>
</html>


VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       If Not Page.IsPostBack Then
           SetInitialRowgrid()
       End If
   End Sub
   Private Sub SetInitialRowgrid()
       Dim dt As New DataTable()
       Dim dr As DataRow = Nothing
 
       dt.Columns.Add(New DataColumn("d1", GetType(String)))
       dt.Columns.Add(New DataColumn("d2", GetType(String)))
       dt.Columns.Add(New DataColumn("d3", GetType(String)))
       dr = dt.NewRow()
       dr("d1") = String.Empty
       dr("d2") = String.Empty
       dr("d3") = String.Empty
       dt.Rows.Add(dr)
       ViewState("IALV") = dt
       RadGrid1.DataSource = dt
       RadGrid1.DataBind()
 
   End Sub
 
   Private Sub AddNewRowToGrid()
 
       Dim rowIndex As Integer = 0
       If ViewState("IALV") IsNot Nothing Then
           Dim dtCurrentTable As DataTable = DirectCast(ViewState("IALV"), DataTable)
           Dim drCurrentRow As DataRow = Nothing
           If dtCurrentTable.Rows.Count > 0 Then
               For i As Integer = 1 To dtCurrentTable.Rows.Count
                   Dim box1 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrno_IT"), TextBox)
                   Dim box2 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("isocd_IT"), TextBox)
                   Dim box3 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrsize_IT"), TextBox)
                   drCurrentRow = dtCurrentTable.NewRow()
                   dtCurrentTable.Rows(i - 1)("d1") = box1.Text
                   dtCurrentTable.Rows(i - 1)("d2") = box2.Text
                   dtCurrentTable.Rows(i - 1)("d3") = box3.Text
                   rowIndex += 1
               Next
               dtCurrentTable.Rows.Add(drCurrentRow)
               ViewState("IALV") = dtCurrentTable
               RadGrid1.DataSource = dtCurrentTable
               RadGrid1.DataBind()
           End If
       Else
           Response.Write("ViewState is null")
       End If
 
       SetPreviousDatagrid()
 
   End Sub
   Private Sub SetPreviousDatagrid()
       Dim rowIndex As Integer = 0
       If ViewState("IALV") IsNot Nothing Then
           Dim dt As DataTable = DirectCast(ViewState("IALV"), DataTable)
           If dt.Rows.Count > 0 Then
               For i As Integer = 0 To dt.Rows.Count - 1
                   Dim box1 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrno_IT"), TextBox)
                   Dim box2 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("isocd_IT"), TextBox)
                   Dim box3 As TextBox = DirectCast(RadGrid1.Items(rowIndex).FindControl("ctrsize_IT"), TextBox)
                   box1.Text = dt.Rows(i)("d1").ToString()
                   box2.Text = dt.Rows(i)("d2").ToString()
                   box3.Text = dt.Rows(i)("d3").ToString()
                   rowIndex += 1
               Next
           End If
       End If
   End Sub
   Protected Sub rowrem_IT_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim lb As LinkButton = DirectCast(sender, LinkButton)
       Dim gvRow As GridDataItem = DirectCast(lb.NamingContainer, GridDataItem)
       Dim rowID As Integer = gvRow.ItemIndex + 1
       If ViewState("IALV") IsNot Nothing Then
           Dim dt As DataTable = DirectCast(ViewState("IALV"), DataTable)
           If dt.Rows.Count > 1 Then
               If gvRow.ItemIndex < dt.Rows.Count - 1 Then
                   dt.Rows.Remove(dt.Rows(rowID))
               End If
           End If
           ViewState("IALV") = dt
           RadGrid1.DataSource = dt
           RadGrid1.DataBind()
       End If
       SetPreviousDatagrid()
   End Sub
 
   Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
       AddNewRowToGrid()
   End Sub
0
Accepted
Radoslav
Telerik team
answered on 24 Nov 2010, 10:18 AM
Hi Salvio,

I am sending you a simple example based on your code which demonstrates the desired functionality. Please check it out and let me know if it helps you.

All the best,
Radoslav
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
salvio
Top achievements
Rank 1
answered on 26 Nov 2010, 07:05 AM
Hi it's working fine,
Thank You Radoslav and Telerik Team.
0
Oliver
Top achievements
Rank 1
answered on 30 Nov 2010, 01:03 PM
i need a sample in EF4!
0
Radoslav
Telerik team
answered on 01 Dec 2010, 04:10 PM
Hi Oliver,

Could you please check out following online examples and let me know if they help you:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/entityframeworkcrud/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/automaticoperations/efdatabinding/defaultcs.aspx

Kind regards,
Radoslav
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
salvio
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
salvio
Top achievements
Rank 1
Iana Tsolova
Telerik team
Radoslav
Telerik team
Oliver
Top achievements
Rank 1
Share this question
or