共用方式為


第 4 課:以程式設計方式更新報表定義

既然已經使用報表欄位從報表伺服器載入報表定義,並且您具有對它的參考,就必須更新報表定義。在此範例中,您將更新報表的 Description 屬性。

更新報表定義

  • 以下列程式碼取代 Program.cs 檔案 (在 Visual Basic 中為 Module1.vb) 中 UpdateReportDefinition() 方法的程式碼:

    private void UpdateReportDefinition()
    {
        System.Console.WriteLine("Updating Report Definition");
    
        // Create a list of the Items Choices for the Report. The 
        // ItemsChoiceType80 enum represents all the properties
        // available in the report and the ItemsElementName 
        // represents the properties that exist in the current 
        // instance of the report.
        List<ItemsChoiceType80> _reportItems = 
            new List<ItemsChoiceType80>(_report.ItemsElementName);
    
        // Locate the index for the Description property
        int index = _reportItems.IndexOf(
            ItemsChoiceType80.Description);
    
        // The Description item is of type StringLocIDType, so 
        // cast the item type first and then assign new value.
        System.Console.WriteLine("- Old Description: " + 
            ((StringLocIDType)_report.Items[index]).Value );
    
        // Update the Description for the Report
        ((StringLocIDType)_report.Items[index]).Value = 
            "New Report Description";
    
        System.Console.WriteLine("- New Description: " + 
            ((StringLocIDType)_report.Items[index]).Value );
    }
    
    Private Sub UpdateReportDefinition()
    
        System.Console.WriteLine("Updating Report Definition")
    
        'Create a list of the Items Choices for the Report. The 
        'ItemsChoiceType80 enum represents all the properties
        'available in the report and the ItemsElementName 
        'represents the properties that exist in the current 
        'instance of the report.
        Dim reportItems As List(Of ItemsChoiceType80) = _
            New List(Of ItemsChoiceType80)(m_report.ItemsElementName)
    
        'Locate the index for the Description property
        Dim index As Integer = _
            reportItems.IndexOf(ItemsChoiceType80.Description)
    
        'The Description item is of type StringLocIDType, so 
        'cast the item type first and then assign new value.
        System.Console.WriteLine("- Old Description: " & _
            DirectCast(m_report.Items(index), StringLocIDType).Value)
    
        'Update the Description for the Report
        DirectCast(m_report.Items(index), StringLocIDType).Value = _
            "New Report Description"
    
        System.Console.WriteLine("- New Description: " & _
            DirectCast(m_report.Items(index), StringLocIDType).Value)
    
    End Sub
    

下一課

在下一課中,會將已更新的報表定義存回報表伺服器上。請參閱<第 5 課:將報表定義發行到報表伺服器>。