telerik 报告:以编程方式创建的表中的可变列宽

发布于 2024-11-11 03:30:51 字数 2107 浏览 5 评论 0原文

我正在 Telerik 报告中动态创建表的列。 这是代码:

            //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) || !_objectInstances.First().Dictionary.ContainsKey(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 = "=Fields." + attribute.ColumnName;
            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});
        }

问题是列宽是固定的。 如果我尝试使用 textbox.Dock = DockStyle.Fill; 我收到对象引用错误。 有没有办法根据内容自动调整列宽?

I'm dynamically creating the columns of a table in a telerik report.
This is the code:

            //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) || !_objectInstances.First().Dictionary.ContainsKey(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 = "=Fields." + attribute.ColumnName;
            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});
        }

The problem with this is that the column width is fixed.
If I try to use textbox.Dock = DockStyle.Fill;
I get an object reference error.
Is there a way to automatically adjust the column width according to the content ?

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

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

发布评论

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

评论(1

海拔太高太耀眼 2024-11-18 03:30:51
 if (dt.Columns[i].ColumnName == "Employee Number")
            {
                textBoxTable.Size = new SizeU(Unit.Inch(.5), Unit.Inch(0.3));
            }
            if (dt.Columns[i].ColumnName == "Employee Name")
            {
                textboxGroup.Size = new SizeU(Unit.Inch(2.1), Unit.Inch(0.3));
            }
            else
            {
                textBoxTable.Size = new SizeU(Unit.Inch(.90), Unit.Inch(0.3));
            }
            if (dt.Columns[i].ColumnName.ToLower().Contains("share"))
            {
                textBoxTable.Style.Color = Color.Chocolate;
                textboxGroup.Size = new SizeU(Unit.Inch(0.9), Unit.Inch(0.3));
            }
 if (dt.Columns[i].ColumnName == "Employee Number")
            {
                textBoxTable.Size = new SizeU(Unit.Inch(.5), Unit.Inch(0.3));
            }
            if (dt.Columns[i].ColumnName == "Employee Name")
            {
                textboxGroup.Size = new SizeU(Unit.Inch(2.1), Unit.Inch(0.3));
            }
            else
            {
                textBoxTable.Size = new SizeU(Unit.Inch(.90), Unit.Inch(0.3));
            }
            if (dt.Columns[i].ColumnName.ToLower().Contains("share"))
            {
                textBoxTable.Style.Color = Color.Chocolate;
                textboxGroup.Size = new SizeU(Unit.Inch(0.9), Unit.Inch(0.3));
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文