VSTO Excel: ¿Cómo hacer un mapeo de columnas personalizado con ListObject?

Tengo un modelo como este:

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?

Gracias.

preguntado el 08 de noviembre de 11 a las 14:11

1 Respuestas

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.

respondido 14 nov., 11:12

Thanks. So simple. I don't know how I missed that! - Ucodia

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.