BeforeAddDataBoundRowEventArgs 介面
提供 BeforeAddDataBoundRow 事件的資料。
命名空間: Microsoft.Office.Tools.Excel
組件: Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)
語法
'宣告
<GuidAttribute("15ffd73e-605e-4d8a-8426-4ddbb71a62f1")> _
Public Interface BeforeAddDataBoundRowEventArgs
[GuidAttribute("15ffd73e-605e-4d8a-8426-4ddbb71a62f1")]
public interface BeforeAddDataBoundRowEventArgs
BeforeAddDataBoundRowEventArgs 型別會公開下列成員。
屬性
名稱 | 說明 | |
---|---|---|
Cancel | 取得或設定值,指出新資料列的加入動作是否應該取消。 | |
Item | 取得要加入至資料來源的資料列。 |
回頁首
備註
在新資料列加入至與 ListObject 繫結的資料來源之前,發生 BeforeAddDataBoundRow 事件。
範例
下列程式碼範例會建立 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.");
}