ListObject.BeforeAddDataBoundRow (Evento)
Se produce inmediatamente antes de un intento por agregar una nueva fila a un control ListObject que se enlaza a los datos.
Espacio de nombres: Microsoft.Office.Tools.Excel
Ensamblado: Microsoft.Office.Tools.Excel (en Microsoft.Office.Tools.Excel.dll)
Sintaxis
'Declaración
Event BeforeAddDataBoundRow As BeforeAddDataBoundRowEventHandler
event BeforeAddDataBoundRowEventHandler BeforeAddDataBoundRow
Comentarios
Se provoca este evento sólo si el control ListObject se enlaza a los datos.
Se provoca este evento sólo cuando se agrega una nueva fila a través de la interfaz de usuario de Microsoft Office Excel. No se provoca cuando se agrega una nueva fila mediante programación.
Controle este evento para realizar validación adicional o cancelar la adición de una fila.
Ejemplos
El ejemplo de código siguiente crea DataTable y ListObject, y enlaza el control ListObject al control DataTable. A continuación, crea un controlador de eventos BeforeAddDataBoundRow. Para probar el evento, agregue manualmente una nueva fila a ListObject en la hoja 1. El controlador de eventos quita la fila y muestra un mensaje.
Se trata de una versión para una personalización en el nivel del documento.
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", missing], "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.");
}
Se trata de una versión para un complemento en el nivel de la aplicación. Para usar este ejemplo, agregue la directiva using System.Data; si utiliza C# o la instrucción Imports System.Data si utiliza 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", missing], "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.");
}
Seguridad de .NET Framework
- Plena confianza para el llamador inmediato. Un código de confianza parcial no puede utilizar este miembro. Para obtener más información, vea Utilizar bibliotecas de código que no es de plena confianza.