ListObject.BeforeAddDataBoundRow 事件
在嘗試將新的列加入至繫結至資料的 ListObject 控制項之前發生。
命名空間: Microsoft.Office.Tools.Excel
組件: Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)
語法
'宣告
Event BeforeAddDataBoundRow As BeforeAddDataBoundRowEventHandler
event BeforeAddDataBoundRowEventHandler BeforeAddDataBoundRow
備註
這個事件只在 ListObject 控制項已繫結至資料時引發。
這個事件只在透過 Microsoft Office Excel 使用者介面加入新列時引發。 當新列以程式設計方式加入時就不會引發事件。
可處理這個事件以執行額外的驗證,或取消新列的加入。
範例
下列程式碼範例會建立 DataTable 和 ListObject,並將 ListObject 繫結至 DataTable。 接著會建立 BeforeAddDataBoundRow 事件處理常式。 若要測試此事件,請以手動方式加入一個新資料列至工作表 1 上的 ListObject。 事件處理常式會移除資料列,並顯示訊息。
這是示範文件層級自訂的版本。
WithEvents BeforeAddDataBoundRowList As _
Microsoft.Office.Tools.Excel.ListObject
Private Sub ListObject_BeforeAddDataBoundRow()
' Create a new DataSet and DataTable.
Dim ds As New DataSet()
Dim dt As DataTable = ds.Tables.Add("Customers")
dt.Columns.Add(New DataColumn("LastName"))
dt.Columns.Add(New DataColumn("FirstName"))
' Add a new row to the DataTable.
Dim dr As DataRow = dt.NewRow()
dr("LastName") = "Chan"
dr("FirstName") = "Gareth"
dt.Rows.Add(dr)
' Create a list object.
BeforeAddDataBoundRowList = _
Me.Controls.AddListObject(Me.Range("A1"), _
"BeforeAddDataBoundRowList")
' Bind the list object to the DataTable.
BeforeAddDataBoundRowList.AutoSetDataBoundColumnHeaders = True
BeforeAddDataBoundRowList.SetDataBinding(ds, "Customers", _
"LastName", "FirstName")
End Sub
Private Sub List1_BeforeAddDataBoundRow(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Excel.BeforeAddDataBoundRowEventArgs) _
Handles BeforeAddDataBoundRowList.BeforeAddDataBoundRow
e.Cancel = True
MessageBox.Show("This data is read-only.")
End Sub
private void ListObject_BeforeAddDataBoundRow()
{
// Create a new DataSet and DataTable.
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("FirstName"));
// Add a new row to the DataTable.
DataRow dr = dt.NewRow();
dr["LastName"] = "Chan";
dr["FirstName"] = "Gareth";
dt.Rows.Add(dr);
// 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.BeforeAddDataBoundRow += new
Microsoft.Office.Tools.Excel.
BeforeAddDataBoundRowEventHandler(
list1_BeforeAddDataBoundRow);
}
void list1_BeforeAddDataBoundRow(object sender,
Microsoft.Office.Tools.Excel.BeforeAddDataBoundRowEventArgs e)
{
e.Cancel = true;
MessageBox.Show("This data is read-only.");
}
這是示範應用程式層級增益集的版本。 若要使用這個程式碼範例,請加入 using System.Data; 指示詞 (如果您使用的是 C#) 或 Imports System.Data 陳述式 (如果您使用的是 Visual Basic)。
WithEvents BeforeAddDataBoundRowList As ListObject
Private Sub ListObject_BeforeAddDataBoundRow()
' Create a new DataSet and DataTable.
Dim ds As New DataSet()
Dim dt As DataTable = ds.Tables.Add("Customers")
dt.Columns.Add(New DataColumn("LastName"))
dt.Columns.Add(New DataColumn("FirstName"))
' Add a new row to the DataTable.
Dim dr As DataRow = dt.NewRow()
dr("LastName") = "Chan"
dr("FirstName") = "Gareth"
dt.Rows.Add(dr)
' 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)
BeforeAddDataBoundRowList = _
vstoWorksheet.Controls.AddListObject( _
vstoWorksheet.Range("A1"), _
"BeforeAddDataBoundRowList")
' Bind the list object to the DataTable.
BeforeAddDataBoundRowList.AutoSetDataBoundColumnHeaders = True
BeforeAddDataBoundRowList.SetDataBinding(ds, "Customers", _
"LastName", "FirstName")
End Sub
Private Sub List1_BeforeAddDataBoundRow(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Excel.BeforeAddDataBoundRowEventArgs) _
Handles BeforeAddDataBoundRowList.BeforeAddDataBoundRow
e.Cancel = True
System.Windows.Forms.MessageBox.Show("This data is read-only.")
End Sub
private void ListObject_BeforeAddDataBoundRow()
{
// Create a new DataSet and DataTable.
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Customers");
dt.Columns.Add(new DataColumn("LastName"));
dt.Columns.Add(new DataColumn("FirstName"));
// Add a new row to the DataTable.
DataRow dr = dt.NewRow();
dr["LastName"] = "Chan";
dr["FirstName"] = "Gareth";
dt.Rows.Add(dr);
// 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.BeforeAddDataBoundRow += new
BeforeAddDataBoundRowEventHandler(
list1_BeforeAddDataBoundRow);
}
void list1_BeforeAddDataBoundRow(object sender,
Microsoft.Office.Tools.Excel.BeforeAddDataBoundRowEventArgs e)
{
e.Cancel = true;
System.Windows.Forms.MessageBox.Show("This data is read-only.");
}
.NET Framework 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。