Metoda ListObject.SetDataBinding — (Object, String, array<String )
Powiązanie ListObject formant, aby członek danych określonego źródła danych i pokazuje tylko określonych kolumn tego członka danych.
Przestrzeń nazw: Microsoft.Office.Tools.Excel
Zestaw: Microsoft.Office.Tools.Excel (w Microsoft.Office.Tools.Excel.dll)
Składnia
'Deklaracja
Sub SetDataBinding ( _
dataSource As Object, _
dataMember As String, _
ParamArray mappedColumns As String() _
)
void SetDataBinding(
Object dataSource,
string dataMember,
params string[] mappedColumns
)
Parametry
- dataSource
Typ: System.Object
Obiekt do użytku jako źródła danych dla ListObject kontroli.
- dataMember
Typ: System.String
DataMember , Który określa właściwość do powiązania w obrębie obiektu zwróconego przez DataSource.
- mappedColumns
Typ: array<System.String[]
Nazwy kolumn danych element członkowski, który ma być wyświetlany w ListObject kontroli.Aby dodać kolumnę niezamapowane, obejmują pusty ciąg znaków w tablicy.
Wyjątki
Wyjątek | Warunek |
---|---|
SetDataBindingFailedException | Nie można powiązać z określonym źródłem danych. |
ArgumentException | Jeden lub więcej argumentów jest nieprawidłowych. |
ArgumentNullException | The dataSource argument is nullodwołanie o wartości null (Nothing w języku Visual Basic). |
Uwagi
dataSource Parametr może być dowolny obiekt, który implementuje IList, IListSource, IBindingList, lub IEnumerable.
dataMember Parametr musi mieć źródło danych, która zwraca kolekcję które można powiązać z właściwością.Na przykład DataSet źródło zawiera tabele jako elementy danych.
Przykłady
Poniższy przykład kodu tworzy DataSet, DataTablei ListObject.Następnie wiąże ListObject do DataSet i DataTable, ale obejmuje tylko jedną z dwóch kolumn tabeli możliwe w ListObject.
W tym przykładzie jest dla dostosowania poziomu dokumentu.
Private Sub ListObject_SetDataBinding3()
Dim Ages As Integer() = {32, 44, 28, 61}
Dim Names As String() = {"Reggie", "Sally", _
"Henry", "Christine"}
' Create a data table with two columns.
Dim ds As New DataSet()
Dim table As DataTable = ds.Tables.Add("Customers")
Dim column1 As New DataColumn("Names", GetType(String))
Dim column2 As New DataColumn("Ages", GetType(Integer))
table.Columns.Add(column1)
table.Columns.Add(column2)
' Add the four rows of data to the table.
Dim row As DataRow
Dim i As Integer
For i = 0 To 3
row = table.NewRow()
row("Names") = Names(i)
row("Ages") = Ages(i)
table.Rows.Add(row)
Next i
' Create the list object.
Dim List1 As Microsoft.Office.Tools.Excel.ListObject = _
Me.Controls.AddListObject(Me.Range("A1", "B4"), "List1")
' Bind the list object to the table.
Dim mappedColumn As String() = {"Names"}
List1.SetDataBinding(ds, "Customers", mappedColumn)
End Sub
private void ListObject_SetDataBinding3()
{
int[] Ages = { 32, 44, 28, 61 };
string[] Names = { "Reggie", "Sally", "Henry", "Christine" };
// Create a data table with two columns.
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add("Customers");
DataColumn column1 = new DataColumn("Names", typeof(string));
DataColumn column2 = new DataColumn("Ages", typeof(int));
table.Columns.Add(column1);
table.Columns.Add(column2);
// Add the four rows of data to the table.
DataRow row;
for (int i = 0; i < 4; i++)
{
row = table.NewRow();
row["Names"] = Names[i];
row["Ages"] = Ages[i];
table.Rows.Add(row);
}
Microsoft.Office.Tools.Excel.ListObject list1 =
this.Controls.AddListObject(this.Range["A1", "B4"], "list1");
// Bind the list object to the table.
string[] mappedColumn = { "Names" };
list1.SetDataBinding(ds, "Customers", mappedColumn);
}
Zabezpieczenia programu .NET Framework
- Pełne zaufanie do bezpośredniego wywołującego. Tego elementu członkowskiego nie można używać w kodzie częściowo zaufanym. Aby uzyskać więcej informacji, zobacz Używanie bibliotek pochodzących z częściowo zaufanego kodu.