在 Telerik 报告 C# 中将表绑定到字典

本文关键字:绑定 字典 Telerik 报告 | 更新日期: 2023-09-27 17:57:05

我想将表绑定到对象列表,每个对象都有一个 IDictionary。

public ObjectInstance
{
        public IDictionary<string, object> Dictionary { get; set; }

来源是IEnumerable<ObjectInstance>我试过这个没有成功:

    void table1_ItemDataBinding(object sender, EventArgs e)
    {
        //create two HtmlTextBox items (one for header and one for data) which would be added to the items collection of the table
        Telerik.Reporting.HtmlTextBox textboxGroup;
        Telerik.Reporting.HtmlTextBox textBoxTable;

        //we do not clear the Rows collection, since we have a details row group and need to create columns only
        this.table1.ColumnGroups.Clear();
        this.table1.Body.Columns.Clear();
        this.table1.Body.Rows.Clear();
        int i = 0;
        this.table1.ColumnHeadersPrintOnEveryPage = true;
        var attributes = _objectInstances.First().ObjectType.Attributes;
        foreach (var attribute in attributes)
        {
            if (string.IsNullOrWhiteSpace(attribute.ColumnName)) continue;

            var tableGroupColumn = new Telerik.Reporting.TableGroup();
            this.table1.ColumnGroups.Add(tableGroupColumn);
            this.table1.Body.Columns.Add(new Telerik.Reporting.TableBodyColumn(Unit.Inch(1)));

            textboxGroup = new Telerik.Reporting.HtmlTextBox();
            textboxGroup.Style.BorderColor.Default = Color.Black;
            textboxGroup.Style.BorderStyle.Default = BorderType.Solid;
            textboxGroup.Value = attribute.ColumnName;
            textboxGroup.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
            tableGroupColumn.ReportItem = textboxGroup;

            textBoxTable = new Telerik.Reporting.HtmlTextBox();
            textBoxTable.Style.BorderColor.Default = Color.Black;
            textBoxTable.Style.BorderStyle.Default = BorderType.Solid;
            textBoxTable.Value = "=Dictionary['"" + attribute.ColumnName + "'"]"; //_objectInstances.First()[attribute.ColumnName].ToString();
            textBoxTable.Size = new SizeU(Unit.Inch(1.1), Unit.Inch(0.3));
            this.table1.Body.SetCellContent(0, i++, textBoxTable);
            this.table1.Items.AddRange(new ReportItemBase[] {textBoxTable, textboxGroup});
        }

    }

这是绑定到字典的正确方法

在 Telerik 报告 C# 中将表绑定到字典

尝试:字典。Values.ToList() 作为数据源

我所做的是转换为数据表,并绑定到它:

        var table = new DataTable();            
        //Create table columns
        _objectInstances.First().Columns.ToList().ForEach(a => table.Columns.Add(a, typeof(string)));
        //Create table rows
        _objectInstances.ToList().ForEach(i => table.Rows.Add(i.Dictionary.Values.ToArray()));

        table1.DataSource = table;