Compartilhar via


Evento ListObject.BeforeAddDataBoundRow

Ocorre antes que uma tentativa de adicionar uma nova linha em um controle de ListObject associado a dados.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (em Microsoft.Office.Tools.Excel.dll)

Sintaxe

'Declaração
Event BeforeAddDataBoundRow As BeforeAddDataBoundRowEventHandler
event BeforeAddDataBoundRowEventHandler BeforeAddDataBoundRow

Comentários

Este evento é gerado apenas se o controle de ListObject é associado a dados.

Este evento é gerado apenas quando uma nova linha é adicionada através da interface do usuário do Microsoft Office Excel. Não é gerado quando uma nova linha é adicionada por meio de programação.

Manipular esse evento para executar a validação adicional, ou cancelar a adição de uma linha.

Exemplos

O exemplo de código a seguir cria DataTable e ListObject, e associa ListObject a DataTable. Então cria um manipulador de eventos de BeforeAddDataBoundRow . Para testar o evento, adicionar manualmente uma nova linha a ListObject na folha 1. O manipulador de eventos remove a linha e exibe uma mensagem.

Esta é uma versão para personalização de um documento nível.

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.");
}

Esta versão é para um suplemento ao aplicativo. Para usar este exemplo de código, adicione a política de using System.Data; se você estiver usando C# ou a declaração de Imports System.Data se você estiver usando 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.");
}

Segurança do .NET Framework

Consulte também

Referência

ListObject Interface

Namespace Microsoft.Office.Tools.Excel