共用方式為


ListObject.OriginalDataRestored 事件

當使用者執行不允許的動作時發生,ListObject 控制項接著會還原至原始狀態。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
Event OriginalDataRestored As OriginalDataRestoredEventHandler
event OriginalDataRestoredEventHandler OriginalDataRestored

備註

會引發這個事件之動作的範例,是使用者對唯讀的資料進行變更。 ListObject 控制項會復原變更並以原始值取代。

這個事件以列舉型別 (Enumeration ) 方式提供資訊,列舉型別中包含已還原的項目以及原因。

範例

下列程式碼範例會建立 DataTableListObject,並將 ListObject 繫結至 DataTable。 接著會建立 OriginalDataRestored 事件處理常式。 若要測試事件,請在工作表 1 的 ListObject 上方的資料行字母上按一下滑鼠右鍵,然後在捷徑功能表中按一下 [刪除]。 事件處理常式會取代欄和欄標題資料,並針對每個動作顯示訊息。

這是示範文件層級自訂的版本。

    WithEvents OriginalDataRestoredList As _
        Microsoft.Office.Tools.Excel.ListObject
    Private Sub ListObject_OriginalDataRestored()
        ' Create a new DataSet and DataTable.
        Dim ds As New DataSet()
        Dim dt As DataTable = ds.Tables.Add("Customers")
        Dim lastName As New DataColumn("LastName")
        dt.Columns.Add(lastName)
        dt.Columns.Add(New DataColumn("FirstName"))

        ' Add two new rows to the DataTable.
        Dim dr1 As DataRow = dt.NewRow()
        dr1("LastName") = "Chan"
        dr1("FirstName") = "Gareth"
        dt.Rows.Add(dr1)
        Dim dr2 As DataRow = dt.NewRow()
        dr2("LastName") = "Nitsche"
        dr2("FirstName") = "Sonja"
        dt.Rows.Add(dr2)

        ' Create a list object.
        OriginalDataRestoredList = Me.Controls.AddListObject( _
            Me.Range("A1"), "OriginalDataRestoredList")

        ' Bind the list object to the DataTable.
        OriginalDataRestoredList.AutoSetDataBoundColumnHeaders = True
        OriginalDataRestoredList.SetDataBinding(ds, "Customers", _
            "LastName", "FirstName")
    End Sub

    Private Sub List1_OriginalDataRestored(ByVal sender As Object, _
        ByVal e As Microsoft.Office.Tools.Excel.OriginalDataRestoredEventArgs) _
        Handles OriginalDataRestoredList.OriginalDataRestored
        MessageBox.Show("This data is bound to a data source and " & _
        "will be restored. This change is: " & e.ChangeType.ToString() & _
        ". The reason is: " & e.ChangeReason.ToString() + ".")
    End Sub

private void ListObject_OriginalDataRestored()
{
    // Create a new DataSet and DataTable.
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Customers");
    DataColumn lastName = new DataColumn("LastName");
    dt.Columns.Add(lastName);
    dt.Columns.Add(new DataColumn("FirstName"));

    // Add two new rows to the DataTable.
    DataRow dr1 = dt.NewRow();
    dr1["LastName"] = "Chan";
    dr1["FirstName"] = "Gareth";
    dt.Rows.Add(dr1);
    DataRow dr2 = dt.NewRow();
    dr2["LastName"] = "Nitsche";
    dr2["FirstName"] = "Sonja";
    dt.Rows.Add(dr2);

    // Create a list object.
    Microsoft.Office.Tools.Excel.ListObject list1 = 
        this.Controls.AddListObject(
        this.Range["A1"], "list1");

    // Bind the list object to the DataTable.
    list1.AutoSetDataBoundColumnHeaders = true;
    list1.SetDataBinding(ds, "Customers", "LastName",
        "FirstName");

    // Create the event handler.
    list1.OriginalDataRestored += new 
        Microsoft.Office.Tools.Excel.
        OriginalDataRestoredEventHandler(list1_OriginalDataRestored);
}

void list1_OriginalDataRestored(object sender, 
    Microsoft.Office.Tools.Excel.OriginalDataRestoredEventArgs e)
{
    MessageBox.Show("This data is bound to a data source and " +
    "will be restored. This change is: " + e.ChangeType.ToString() +
    ". The reason is: " + e.ChangeReason.ToString() + ".");
}

這是示範應用程式層級增益集的版本。 若要使用這個程式碼範例,請加入 using System.Data; 指示詞 (如果您使用的是 C#) 或 Imports System.Data 陳述式 (如果您使用的是 Visual Basic)。

WithEvents OriginalDataRestoredList As ListObject
Private Sub ListObject_OriginalDataRestored()
    ' Create a new DataSet and DataTable.
    Dim ds As New DataSet()
    Dim dt As DataTable = ds.Tables.Add("Customers")
    Dim lastName As New DataColumn("LastName")
    dt.Columns.Add(lastName)
    dt.Columns.Add(New DataColumn("FirstName"))

    ' Add two new rows to the DataTable.
    Dim dr1 As DataRow = dt.NewRow()
    dr1("LastName") = "Chan"
    dr1("FirstName") = "Gareth"
    dt.Rows.Add(dr1)
    Dim dr2 As DataRow = dt.NewRow()
    dr2("LastName") = "Nitsche"
    dr2("FirstName") = "Sonja"
    dt.Rows.Add(dr2)

    ' Create a list object.
    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Me.Application.Worksheets(1)
    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)
    OriginalDataRestoredList = vstoWorksheet.Controls.AddListObject( _
        vstoWorksheet.Range("A1"), "OriginalDataRestoredList")

    ' Bind the list object to the DataTable.
    OriginalDataRestoredList.AutoSetDataBoundColumnHeaders = True
    OriginalDataRestoredList.SetDataBinding(ds, "Customers", _
        "LastName", "FirstName")
End Sub

Private Sub List1_OriginalDataRestored(ByVal sender As Object, _
    ByVal e As Microsoft.Office.Tools.Excel.OriginalDataRestoredEventArgs) _
    Handles OriginalDataRestoredList.OriginalDataRestored
    System.Windows.Forms.MessageBox.Show( _
        "This data is bound to a data source and " & _
        "will be restored. This change is: " & e.ChangeType.ToString() & _
        ". The reason is: " & e.ChangeReason.ToString() + ".")
End Sub
private void ListObject_OriginalDataRestored()
{
    // Create a new DataSet and DataTable.
    DataSet ds = new DataSet();
    DataTable dt = ds.Tables.Add("Customers");
    DataColumn lastName = new DataColumn("LastName");
    dt.Columns.Add(lastName);
    dt.Columns.Add(new DataColumn("FirstName"));

    // Add two new rows to the DataTable.
    DataRow dr1 = dt.NewRow();
    dr1["LastName"] = "Chan";
    dr1["FirstName"] = "Gareth";
    dt.Rows.Add(dr1);
    DataRow dr2 = dt.NewRow();
    dr2["LastName"] = "Nitsche";
    dr2["FirstName"] = "Sonja";
    dt.Rows.Add(dr2);

    // Create a list object.
    Worksheet vstoWorksheet =
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    ListObject list1 =
        vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1"], "list1");

    // Bind the list object to the DataTable.
    list1.AutoSetDataBoundColumnHeaders = true;
    list1.SetDataBinding(ds, "Customers", "LastName",
        "FirstName");

    // Create the event handler.
    list1.OriginalDataRestored += new
        OriginalDataRestoredEventHandler(list1_OriginalDataRestored);
}

void list1_OriginalDataRestored(object sender,
    Microsoft.Office.Tools.Excel.OriginalDataRestoredEventArgs e)
{
    System.Windows.Forms.MessageBox.Show("This data is bound to a data source and " +
    "will be restored. This change is: " + e.ChangeType.ToString() +
    ". The reason is: " + e.ChangeReason.ToString() + ".");
}

.NET Framework 安全性

請參閱

參考

ListObject 介面

Microsoft.Office.Tools.Excel 命名空間