逐步解說:VSTO 載入宏專案中的簡單數據系結

您可以將資料繫結至 VSTO 增益集專案中的主控制項和 Windows Form 控制項。 本逐步解說示範如何將控制項加入 Microsoft Office Word 文件,以及在執行階段將控制項繫結至資料。

適用於: 本主題中的資訊適用於 Word 的 VSTO 載入宏專案。 如需詳細資訊,請參閱 Office 應用程式 lication 和項目類型所提供的功能。

本逐步解說將說明下列工作:

  • 在設計階段將 ContentControl 加入文件。

  • 建立可將控制項連接到資料集執行個體的 BindingSource

  • 讓使用者能夠捲動記錄及在控制項中檢視它們。

注意

在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置: 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱將 IDE 個人化

必要條件

您需要下列元件才能完成這個逐步解說:

建立新專案

第一步是建立 Word VSTO 增益集專案。

建立新的專案

  1. 使用 Visual Basic 或 C#,建立名稱為 [ 從資料庫填入文件] 的 Word VSTO 增益集專案。

    如需詳細資訊,請參閱 如何:在Visual Studio中建立 Office專案。

    Visual Studio 會開啟 ThisAddIn.vbThisAddIn.cs 檔案,並將 [填入的檔] 從資料庫專案新增方案總管

  2. 如果您的專案以 .NET Framework 4 或 .NET Framework 4.5 為目標,請新增 Microsoft.Office.Tools.Word.v4.0.Utilities.dll 元件的參考。 本逐步解說稍後會需要用到此參考,以透過程式設計的方式將 Windows Forms 控制項加入文件。

建立資料來源

使用 [ 資料來源 ] 視窗將型別資料集加入專案。

將具類型資料集加入專案

  1. 如果看不到 [數據源] 視窗,請在功能表欄上選擇 [檢視>其他 Windows>數據源] 來顯示它。

  2. 選擇 [ 加入新資料來源 ] 以啟動 [ 資料來源組態精靈]。

  3. 按一下 [ 資料庫],然後按 [ 下一步]。

  4. 如果您有 AdventureWorksLT 資料庫的現有連接,請選擇這個連接,然後按 [ 下一步]。

    否則,請按一下 [ 新增連接],然後使用 [ 加入連接 ] 對話方塊建立新的連接。 如需詳細資訊,請參閱新增連線

  5. 在 [ 將連接字串儲存到應用程式組態檔 ] 頁面上,按 [ 下一步]。

  6. 在 [ 選擇您的資料庫物件 ] 頁面中,展開 [ 資料表 ],然後選取 [ Customer (SalesLT)]。

  7. 按一下完成

    AdventureWorksLTDataSet.xsd 檔案會新增至 方案總管。 這個檔案會定義下列項目:

    • 具類型資料集,名稱為 AdventureWorksLTDataSet。 此資料集代表 AdventureWorksLT 資料庫中 Customer (SalesLT) 資料表的內容。

    • 名為 CustomerTableAdapter的 TableAdapter。 這個 TableAdapter 可用來讀取和寫入 中的數據 AdventureWorksLTDataSet。 如需詳細資訊,請參閱 TableAdapter 概觀

      您將在本逐步解說稍後用到這兩個物件。

建立控件並將控件系結至數據

本逐步解說中用來檢視資料庫記錄的介面是基本的,而且會直接在檔內建立。 一個 ContentControl 一次顯示單一資料庫記錄,而兩個 Button 控制項可讓您來回捲動記錄。 內容控制項使用 BindingSource 連接到資料庫。

如需將控件系結至數據的詳細資訊,請參閱 將數據系結至 Office 方案中的控件。

在文件中建立介面

  1. ThisAddIn 類別中,宣告下列控制項來顯示和捲動 Customer 資料庫的 AdventureWorksLTDataSet 資料表。

    private AdventureWorksLTDataSet adventureWorksDataSet;
    private AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter customerTableAdapter;
    private System.Windows.Forms.BindingSource customerBindingSource;
    private Microsoft.Office.Tools.Word.RichTextContentControl customerContentControl;
    private Microsoft.Office.Tools.Word.Controls.Button button1;
    private Microsoft.Office.Tools.Word.Controls.Button button2;
    
  2. ThisAddIn_Startup 方法中,加入下列程式碼來初始化資料集、在資料集填入來自 AdventureWorksLTDataSet 資料庫的資訊。

    this.adventureWorksDataSet = new AdventureWorksLTDataSet();
    this.customerTableAdapter = new AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter();
    this.customerTableAdapter.Fill(this.adventureWorksDataSet.Customer);
    this.customerBindingSource = new System.Windows.Forms.BindingSource();
    
  3. 將下列程式碼新增至 ThisAddIn_Startup 方法。 這會產生可擴充文件的主項目。 如需詳細資訊,請參閱 在運行時間擴充 VSTO 載入宏中的 Word 檔和 Excel 活頁簿。

    Word.Document currentDocument = this.Application.ActiveDocument;
    
    Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
    
  4. 在文件開頭定義數個範圍。 這些範圍會識別插入文字和放置控制項的位置。

    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    extendedDocument.Paragraphs[1].Range.Text = 
        "The companies listed in the AdventureWorksLT database:   \n";
    extendedDocument.Paragraphs[2].Range.Text = "  "; 
    
    Word.Range range1 = extendedDocument.Paragraphs[2].Range.Characters.First;
    Word.Range range2 = extendedDocument.Paragraphs[2].Range.Characters.Last;
    Word.Range range3 = extendedDocument.Paragraphs[1].Range.Characters.Last;
    
  5. 將介面控制項加入先前定義的範圍。

    this.button1 = extendedDocument.Controls.AddButton(range1, 60, 15, "1");
    this.button1.Text = "Previous";
    this.button2 = extendedDocument.Controls.AddButton(range2, 60, 15, "2");
    this.button2.Text = "Next";
    
    this.customerContentControl = extendedDocument.Controls.AddRichTextContentControl(
        range3, "richTextContentControl1");
    
  6. 使用 AdventureWorksLTDataSet 將內容控制項繫結至 BindingSource。 C# 開發人員請加入 Button 控制項的兩個事件處理常式。

    this.customerBindingSource.DataSource = this.adventureWorksDataSet.Customer;
    this.customerContentControl.DataBindings.Add("Text", this.customerBindingSource, 
        "CompanyName", true, this.customerContentControl.DataBindings.DefaultDataSourceUpdateMode);
    
    this.button1.Click += new EventHandler(button1_Click);
    this.button2.Click += new EventHandler(button2_Click);
    
  7. 加入下列程式碼來瀏覽資料庫記錄。

    void button1_Click(object sender, EventArgs e)
    {
        this.customerBindingSource.MovePrevious();
    }
    
    void button2_Click(object sender, EventArgs e)
    {
        this.customerBindingSource.MoveNext();
    }
    

測試載入宏

當您開啟 Word 時,內容控制項會顯示來自 AdventureWorksLTDataSet 資料集的資料。 按 [ 下一個 ] 和 [ 前一個 ] 按鈕,即可捲動資料庫記錄。

測試 VSTO 增益集

  1. 請按 F5

    會建立名為 customerContentControl 的內容控制項並填入資料。 同時間,名為 adventureWorksLTDataSet 的資料集物件,和名為 BindingSourcecustomerBindingSource 會加入專案。 ContentControl 已繫結至 BindingSource,而後者又繫結至資料集物件。

  2. 按 [ 下一個 ] 和 [ 前一個 ] 按鈕,即可捲動資料庫記錄。