将表绑定到 telerik reporting c# 中的字典

发布于 2024-11-08 11:56:32 字数 2365 浏览 2 评论 0原文

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

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

源是 IEnumerable 我尝试过这个但没有成功:

    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});
        }


    }

Which is the right way to bind to adictionary

I want to bind a table to a list of objects, each object has a IDictionary.

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

The source is a IEnumerable<ObjectInstance>
I've tried this with no success:

    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});
        }


    }

Which is the correct way to bind to a dictionary

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够运 2024-11-15 11:56:32

尝试:dictionary.Values.ToList()作为您的数据源

Try: dictionary.Values.ToList() as your datasource

迷乱花海 2024-11-15 11:56:32

我所做的是将其转换为 DataTable,然后绑定到它:

        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;

What I did is convert to a DataTable, and bind to it instead:

        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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文