Copy Records between Two Table Panels

In this article, I demonstrate how to copy records between two identical table panels on the same page where the destination table may also contain data.
- Gil Givati, CEO of Efficens Software Ltd.

November 8, 2007
Iron Speed Designer V5.X

Introduction

A client recently contacted me for help creating two identical table panels. One table panel contained data and the other panel was empty. The customer wanted to copy selected data from one panel to the other. In this article, I demonstrate how to copy records between two identical table panels on the same page where the destination table may also contain data.


A source table panel in an Iron Speed Designer generated application.

Let’s begin with two identical tables: SourceTBL and TargetTBL.

Each table has three columns:
1. PK - the primary key generated automatically by the database
2. Name - a string column
3. Age - and a numeric column.

Before we discuss the code in this function I would like to review the page life cycle, specifically LoadData and DataBind. LoadData retrieves information from the database when a page is loaded. DataBind links the information retrieved to controls on a page. Now let’s walk through the steps to modify the TargetTBL data source, which includes data for the table panel, before the Databind method is called.

Procedure

Step 1:
Create a new page and place one show table panel and one edit table panel on the page. The show table panel is used to display the SourceTBL data and the Edit Table panel is used to save the copied data in TargetTBL. Now you must decide if you want to allow users to modify the updated information after it has been copied to the TargetTBl table panel. If you want to block this option, change the display properties of each column to literal and make sure you select the "save to database" option in the Page Properties dialog.

Step 2:
Add a button to the SourceTBL table control. This button is used to copy the selected records to the TargetTBL. In this example, this button is called "Button".

Step 3:
Create two public (shared) array lists at the TargetTBlTableControl class. Name the first array list: NewDataToAdd, and the second list: OriginalDataSource. The first list is used to store the selected records to copy for use at the DataBind method of this class. Use the second list to store the original DataSource of the table when data is loaded from the database using the LoadData method.

Once this is complete your top lines of code in this class are:
Public Class TargetTBlTableControl
    Inherits BaseTargetTBlTableControl
 
    Public Shared NewDataToAdd As ArrayList
    Public Shared OriginalDataSource As ArrayList

Step 4:
Override the TargetTBLTableControl LoadData Method by implementing the following code:

Public Overrides Sub loaddata()
    MyBase.LoadData()
    If IsNothing(OriginalDataSource) Then
      If Not IsNothing(DataSource) Then
         OriginalDataSource = New ArrayList(Me.DataSource)
      Else
         OriginalDataSource = New ArrayList
      End If
    Else
      OriginalDataSource = New ArrayList(Me.DataSource)
    End If
End Sub

Once we load the data, we maintain its information in the global OriginalDataSource variable that we declared previously.

Step 5:
In this step we need to override the targetTBLTableControl.DataBind method. Our new DataBind method tests to see if there is new data to add to the DataSource in the NewDataToAdd variable. If new data exists, add these records to this table datasource object and bind the controls for proper display. The code for the new Databind method is:

Public Overrides Sub DataBind()
    If Not IsNothing(NewDataToAdd) Then
        If NewDataToAdd.Count > 0 And Not IsNothing(OriginalDataSource) _
          Then
             OriginalDataSource.AddRange(NewDataToAdd)
             Me.DataSource = _
     DirectCast(OriginalDataSource.ToArray(GetType(TargetTBlRecord)), _
     TargetTBlRecord())
        End If
        NewDataToAdd.Clear()
    End If
    MyBase.DataBind()
End Sub

Step 6:
Tie everything together by writing the button click method! The button click method processes the selected records of the sourceTBl table control. Add the button click method to the NewDataToAdd variable of the TargetTBL table control and call the targetTBL table control Databind to refresh its data.

Public Overrides Sub Button_Click(ByVal sender As Object, _
ByVal args As EventArgs)
    Dim MySelectedSourceRecords() As sourceTblTableControlRow = _
  Me.GetSelectedRecordControls
    Dim TargetControl As TargetTBlTableControl = _
        CType(Me.Page.FindControlRecursively("TargetTblTableControl"), _
  TargetTBlTableControl)
    Dim SourceREcord As sourceTblTableControlRow
    Dim TargetREcord As TargetTBlRecord
    Dim TargetDataList As ArrayList
    TargetDataList = New ArrayList
    For Each SourceREcord In MySelectedSourceRecords
        TargetREcord = New TargetTBlRecord
        TargetREcord.Age = CInt(SourceREcord.Age.Text)
        TargetREcord.Name = SourceREcord.Name.Text
        SourceREcord.sourceTblRecordRowSelection.Checked = False
        TargetDataList.Add(TargetREcord)
    Next
    If Not IsNothing(TargetDataList) And TargetDataList.Count > 0 Then
      If Not IsNothing(TargetTBlTableControl.NewDataToAdd) Then
            TargetTBlTableControl.NewDataToAdd.Clear()
            TargetTBlTableControl.NewDataToAdd = TargetDataList
      Else
            TargetTBlTableControl.NewDataToAdd = New ArrayList
            TargetTBlTableControl.NewDataToAdd = TargetDataList
      End If
      TargetControl.DataBind()
    End If
End Sub

Conclusion

In summary, retrieve the selected record controls using the GetSelectedRecordControls method. Then, for each selected row, create a new TargetRecord, copy the data from the original record, and add it to a list which hosts all of the new records. Once the data copy is complete, and only if there were records, populate the NewDataToAdd variable of the targetTBl table control. Finally, fire the TargetTBL DataBind method to finish the process.

Now that we've completed the six steps above, our page is working and allows us to quickly copy selected information from one table panel to the other using a custom button. For those of you who want to see the complete project, I've uploaded it to the Code Repository technical forum at: http://sjc.ironspeed.com/tool/post/ironspeed/vpost?id=2181229&pid=20729979#post20729979

About the Author

Gil Givati
CEO of Efficens Software Ltd.

Gil has been working with IT systems for the past 17 years from both the infrastructure side of applications and the development side of it. During these years he has served as a database systems team leader for one of the Israeli defense forces software units and Chief technology officer. Gil has also served as the products group manager and CTO in a software house that is representing Sybase Inc., iAnywhere Solutions, Information Builders and other companies. As part of his job he was required to take active part in systems design and deployment. For the past two years Gil has managed Efficens Software and while also taking active part in its projects and products activities.

Gil earned a BA in Business Management from the Derby University in the United Kingdom.



  Privacy Statement