BeforeAddDataBoundRowEventHandler 委派
表示處理 ListObject 的 BeforeAddDataBoundRow 事件的方法。
命名空間: Microsoft.Office.Tools.Excel
組件: Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)
語法
'宣告
Public Delegate Sub BeforeAddDataBoundRowEventHandler ( _
sender As Object, _
e As BeforeAddDataBoundRowEventArgs _
)
public delegate void BeforeAddDataBoundRowEventHandler(
Object sender,
BeforeAddDataBoundRowEventArgs e
)
參數
- sender
型別:System.Object
事件的來源。
- e
型別:Microsoft.Office.Tools.Excel.BeforeAddDataBoundRowEventArgs
包含事件資料的 BeforeAddDataBoundRowEventArgs。
備註
在建立 BeforeAddDataBoundRowEventHandler 委派 (Delegate) 時,您要識別處理事件的方法。若要讓此事件與您的事件處理常式產生關聯,請將委派的執行個體加入至此事件。在您移除該委派之前,每當事件發生,就會呼叫事件處理常式。如需委派的詳細資訊,請參閱事件和委派。
範例
下列程式碼範例會建立 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.");
}