ErrorAddDataBoundRowEventArgs 介面
提供 ErrorAddDataBoundRow 事件的資訊。
命名空間: Microsoft.Office.Tools.Excel
組件: Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)
語法
'宣告
<GuidAttribute("eca34a5d-a43c-4be4-a24a-49d49ae4519b")> _
Public Interface ErrorAddDataBoundRowEventArgs
[GuidAttribute("eca34a5d-a43c-4be4-a24a-49d49ae4519b")]
public interface ErrorAddDataBoundRowEventArgs
ErrorAddDataBoundRowEventArgs 型別會公開下列成員。
屬性
名稱 | 說明 | |
---|---|---|
InnerException | 取得造成目前例外狀況的 Exception 執行個體。 | |
Item | 取得 ListObject 嘗試加入至資料來源的新項目。 | |
Retry | 取得或設定值,表示 ListObject 是否應該嘗試再次加入此項目。 |
回頁首
備註
如果嘗試加入一個新資料列至與 ListObject 繫結的資料來源,而發生例外狀況,ListObject 控制項會引發 ErrorAddDataBoundRow 事件。Item 屬性可用來取得 ListObject 嘗試加入的新項目。InnerException 屬性提供擲回的例外狀況,而 Retry 屬性可用來指出是否應該讓另一個嘗試加入此資料列的行為成功的執行。
範例
下列程式碼範例會建立 DataTable 和 ListObject,並將 ListObject 繫結至 DataTable。接著會建立 ErrorAddDataBoundRow 事件處理常式。若要測試此事件,請以手動方式加入一個新資料列至 ListObject,並輸入姓氏 "Chan" 和名字。此事件處理常式會顯示一個訊息。
這是示範文件層級自訂的範例。
WithEvents ErrorAddDataBoundRowList As _
Microsoft.Office.Tools.Excel.ListObject
Private Sub ListObject_ErrorAddDataBoundRow()
' 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"))
Dim myUC As New UniqueConstraint("CustConstraint", _
lastName)
dt.Constraints.Add(myUC)
' 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.
ErrorAddDataBoundRowList = _
Me.Controls.AddListObject(Me.Range("A1"), _
"ErrorAddDataBoundRowList")
' Bind the list object to the DataTable.
ErrorAddDataBoundRowList.AutoSetDataBoundColumnHeaders = True
ErrorAddDataBoundRowList.SetDataBinding(ds, "Customers", _
"LastName", "FirstName")
End Sub
Private Sub List1_ErrorAddDataBoundRow(ByVal sender As Object, _
ByVal e As Microsoft.Office.Tools.Excel.ErrorAddDataBoundRowEventArgs) _
Handles ErrorAddDataBoundRowList.ErrorAddDataBoundRow
MessageBox.Show("Last names must be unique.")
End Sub
private void ListObject_ErrorAddDataBoundRow()
{
// 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"));
UniqueConstraint myUC = new UniqueConstraint( "CustConstraint",
lastName);
dt.Constraints.Add(myUC);
// 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.ErrorAddDataBoundRow += new
Microsoft.Office.Tools.Excel.
ErrorAddDataBoundRowEventHandler(list1_ErrorAddDataBoundRow);
}
void list1_ErrorAddDataBoundRow(object sender,
Microsoft.Office.Tools.Excel.ErrorAddDataBoundRowEventArgs e)
{
MessageBox.Show("Last names must be unique.");
}