【问题标题】:VSTO Excel: How to make a custom column mapping with ListObject?VSTO Excel:如何使用 ListObject 进行自定义列映射?
【发布时间】:2011-11-08 14:19:05
【问题描述】:

我有一个这样的模型:

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

    // About 100 other properties
}

我从实体框架 EDM 中检索 Instrument 实例。

现在我想做的是能够进行自定义映射以将此数据集绑定到 Excel ListObject。默认情况下,ListObject 将显示 Excel 工作表中的每一列,但我只想按特定顺序显示一些属性。

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

// Custom mapping code...

我尝试使用 SetDataBinding 方法,但它抛出了数据绑定异常。

有人可以帮我实现吗?

谢谢。

【问题讨论】:

    标签: excel data-binding vsto


    【解决方案1】:

    我不知道这是否有帮助,但我为此创建了一个测试项目。测试项目运行没有问题。

    首先,我声明一个测试类:

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

    我有一个函数 getInstruments,它返回一个列表。

    然后我使用此代码将其绑定到工作表:

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

    这将显示一个包含 4 个仪器的工作表,在 A 列和 B 列中显示名称和属性 1。

    我也尝试过使用实体框架,将工作表连接到我的一个数据库,也没有遇到任何问题。

    如果您仍然遇到问题,请提供有关您在使用 SetDataBinding 时遇到的异常的更多信息。

    【讨论】:

    • 谢谢。很简单。我不知道我是怎么错过的!
    猜你喜欢
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多