Imports Telerik.Web.UI
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then

        End If
    End Sub
    Private Sub SetInitialRowgrid()

        If ViewState("IALV") Is Nothing Then
            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
        End If

       
        RadGrid1.DataSource = CType(ViewState("IALV"), DataTable)

    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.Rebind()
            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.RemoveAt(rowID - 1)
                End If
            End If
            ViewState("IALV") = dt
            RadGrid1.Rebind()
        End If
        SetPreviousDatagrid()
    End Sub

    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        AddNewRowToGrid()
    End Sub

    Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
        SetInitialRowgrid()
    End Sub
End Class
