Spread WPF 18
Features / Table / Table Binding
In This Topic
    Table Binding
    In This Topic

    Spread for WPF allows you to bind a table to a datasource using cell-level binding.

    Table binding can be done in two ways:

    Binding Tables Automatically

    You can bind a table to a datasource automatically by using the DataSource property of the ITable Interface and its members. Set the AutoGenerateColumns property of the table to true before setting its DataSource property. Otherwise, current table columns will be retained.

    Refer to the following example where a company’s finance department maintains a database of its employees' work-related travel information, such as flight ID, flight date, source, and destination. The database can be loaded in a table automatically to display the travel details.

     

    Copy Code
    // Table binding when AutoGenerateColumns is set to true.
    GrapeCity.Spreadsheet.IWorksheet sheet = spreadSheet1.Workbook.ActiveSheet;
    // Create table data.
    DataSet ds = new DataSet();
    DataTable flightDetails = new DataTable("FlightDetails");
    // Define columns for the DataTable.
    flightDetails.Columns.Add("Passenger Name");
    flightDetails.Columns.Add("Department");
    flightDetails.Columns.Add("Ticket Type");
    flightDetails.Columns.Add("Flight ID");
    flightDetails.Columns.Add("Flight Date");
    flightDetails.Columns.Add("Flight Src");
    flightDetails.Columns.Add("Flight Dest");
    // Add rows of flight details.
    flightDetails.Rows.Add("Mark", "Sales", "Economy", 7855, new DateTime(2021, 10, 11).ToShortDateString(), "New York", "Tokyo");
    flightDetails.Rows.Add("Sophie", "Services", "Economy", 7426, new DateTime(2021, 10, 12).ToShortDateString(), "London", "Venice");
    flightDetails.Rows.Add("Oliver", "Finance", "Business", 7641, new DateTime(2021, 10, 15).ToShortDateString(), "New Delhi", "Moscow");
    flightDetails.Rows.Add("James", "R&D", "Economy", 7293, new DateTime(2021, 10, 18).ToShortDateString(), "Beijing", "Dubai");
    flightDetails.Rows.Add("Emma", "Marketing", "Business", 7117, new DateTime(2021, 10, 20).ToShortDateString(), "Paris", "Seoul");
    // Add the DataTable to the DataSet.
    ds.Tables.Add(flightDetails);
    // Create table.
    sheet.Cells["A1"].Value = "For Finance Dept. only:";
    GrapeCity.Spreadsheet.ITable table = sheet.Range("A2:G7").CreateTable(true);
    // Set auto generate columns.
    table.AutoGenerateColumns = true;
    // Bind table to the DataSource.
    table.DataSource = ds;
    
    Copy Code
    ' Table binding when AutoGenerateColumns is set to true.
    Dim sheet As GrapeCity.Spreadsheet.IWorksheet = spreadSheet1.Workbook.ActiveSheet
    ' Create table data.
    Dim ds As DataSet = New DataSet()
    Dim flightDetails As DataTable = New DataTable("FlightDetails")
    ' Define columns for the DataTable.
    flightDetails.Columns.Add("Passenger Name")
    flightDetails.Columns.Add("Department")
    flightDetails.Columns.Add("Ticket Type")
    flightDetails.Columns.Add("Flight ID")
    flightDetails.Columns.Add("Flight Date")
    flightDetails.Columns.Add("Flight Src")
    flightDetails.Columns.Add("Flight Dest")
    ' Add rows of flight details.
    flightDetails.Rows.Add("Mark", "Sales", "Economy", 7855, New DateTime(2021, 10, 11).ToShortDateString(), "New York", "Tokyo")
    flightDetails.Rows.Add("Sophie", "Services", "Economy", 7426, New DateTime(2021, 10, 12).ToShortDateString(), "London", "Venice")
    flightDetails.Rows.Add("Oliver", "Finance", "Business", 7641, New DateTime(2021, 10, 15).ToShortDateString(), "New Delhi", "Moscow")
    flightDetails.Rows.Add("James", "R&D", "Economy", 7293, New DateTime(2021, 10, 18).ToShortDateString(), "Beijing", "Dubai")
    flightDetails.Rows.Add("Emma", "Marketing", "Business", 7117, New DateTime(2021, 10, 20).ToShortDateString(), "Paris", "Seoul")
    ' Add the DataTable to the DataSet.
    ds.Tables.Add(flightDetails)
    ' Create table.
    sheet.Cells("A1").Value = "For Finance Dept. only:"
    Dim table As GrapeCity.Spreadsheet.ITable = sheet.Range("A2:G7").CreateTable(True)
    ' Set auto generate columns.
    table.AutoGenerateColumns = True
    ' Bind table to the DataSource.
    table.DataSource = ds
    

    Binding Tables Manually

    You can bind a table to a datasource and manually set its columns by using the DataField property of the ITableColumn Interface. To manually bind a table, the AutoGenerateColumns property must be set to false.

    Considering the example from the previous section, the company can also manually create a table using the existing datasource. This table keeps a record of employees’ unavailability on their flying dates to keep their respective managers informed as shown in the image below.

    Copy Code
    // Table binding when AutoGenerateColumns is set to false.
    // Create table.
    GrapeCity.Spreadsheet.IWorksheet sheet = spreadSheet1.Workbook.ActiveSheet;
    sheet.Cells["A1"].Value = "Unavailable Employees:";
    GrapeCity.Spreadsheet.ITable table2 = sheet.Range("A2:D7").CreateTable(true);
    // Disable auto generate columns.
    table2.AutoGenerateColumns = false;
    // Set data fields in columns.
    table2.TableColumns[0].DataField = "Passenger Name";
    table2.TableColumns[1].DataField = "Department";
    table2.TableColumns[2].DataField = "Flight Dest";
    table2.TableColumns[3].DataField = "Flight Date";
    // Bind table to the existing datasource 'ds' defined in the previous section.
    table2.DataSource = ds;
    
    Copy Code
    ' Table binding when AutoGenerateColumns is set to false.
    ' Create table.
    Dim sheet As GrapeCity.Spreadsheet.IWorksheet = spreadSheet1.Workbook.ActiveSheet
    sheet.Cells("A1").Value = "Unavailable Employees:"
    Dim table2 As GrapeCity.Spreadsheet.ITable = sheet.Range("A2:D7").CreateTable(True)
    ' Disable auto generate columns.
    table2.AutoGenerateColumns = False
    ' Set data fields in columns.
    table2.TableColumns(0).DataField = "Passenger Name"
    table2.TableColumns(1).DataField = "Department"
    table2.TableColumns(2).DataField = "Flight Dest"
    table2.TableColumns(3).DataField = "Flight Date"
    ' Bind table to the existing datasource 'ds' defined in the previous section.
    table2.DataSource = ds