VSTO Excel: How to make a custom column mapping with ListObject?

I have a model like this:

public class Instrument
{
    public string Id { get; set; }
    public string Name { get; set; }

    // About 100 other properties
}

And I retrieve Instrument instances from an Entity Framework EDM.

Now what I would like to do is to be able to make a custom mapping to bind this dataset to a Excel ListObject. By default, ListObject will display every column in the Excel worksheet but I only want to show some properties and in a particular order.

ExcelTools.ListObject instrumentsTable = this.Controls.AddListObject(tableStart, tableName);
instrumentsTable.DataSource = myEDM.Instruments;

// Custom mapping code...

I tried to use the SetDataBinding method but it was throwing a data binding exception.

Do anybody can help me achieving that?

Thanks.

Answers


I don't know if this is helpful, but I created a test project doing this. The test project runs without problems.

First, I declare a test class:

public class Instrument
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }

}

I have a function getInstruments, that returns a List.

Then I bind this to a sheet using this code:

        List<Instrument> list = getInstruments();
        ListObject instrumentsTable = Controls.AddListObject(Range["A1", "B4"], "list1");
        string[] mappedColumns = { "Name", "Property1" };
        instrumentsTable.SetDataBinding(list, string.Empty, mappedColumns);

This displays a sheet with 4 instruments showing Name and Property1 in columns A and B.

I also tried this using Entity framework, connecting the sheet to one of my databases and didn't run into any problems with that either.

If you are still having problems, please give more information about the exception you get when using SetDataBinding.


Need Your Help

Possible to write swift println logs into file too?

swift logging

Is it an easy way to write logs into a text file too? I need a crash log to analyse when something went wrong. But I already use println al around in the code.

java.lang.IllegalStateException: ScrollView can host only one direct child

java android xml android-layout android-ui

I'm simply trying to add the ability to scroll through this layout by adding a scrollview however every time I attempt to load the layout I'm getting an error stating "java.lang.IllegalStateExcepti...