c# - 如何在 C# 中动态分配运行时的表格单元格宽度?

标签 c# devexpress xtrareport

我在 devexpress 报告中有一个函数,它根据 SQL 查询动态创建表:

readonly int[] cellWidth = { 5, 20, 30, 40, 50, 60 };// { 16, 100, 100, 30, 20, 16 };

private XRTable CreateXRTableDetail(DataTable dtAra)
{
    XRTable table = new XRTable();
    table.BeginInit();

    table.LocationFloat = new DevExpress.Utils.PointFloat(0, 5F);
    table.Borders = BorderSide.All;

    int tableHeight = 0;
    int tableWidth = 0;

    for (int i = -1; i < 4; i++)
    {
        XRTableRow row = new XRTableRow();                
        for (int j = 0; j < 6; j++)
        {
            XRTableCell cell = new XRTableCell();
            cell.Padding = 1;

            Unit width = new Unit(cellWidth[j], UnitType.Pixel);

            cell.Width = (int)width.Value;
            cell.Weight = 1;
            cell.TextAlignment = TextAlignment.MiddleCenter;
            tableWidth += cell.Width;

            if (i == -1)//Header
            {
                row.Height = 15;
                cell.Text = dtAra.Columns[j].ColumnName;
                cell.BackColor = Color.Gainsboro;
                cell.Font = new Font("tahoma", 6);
            }
            else
            {
                row.Height = 40;
                cell.Text = dtAra.Rows[i][j].ToString();
                cell.Font = new Font("tahoma", 5);
            }
            row.Cells.Add(cell);
        }

        tableHeight += row.Height;
        table.Rows.Add(row);
    }

    tableWidth = tableWidth / table.Rows.Count;
    table.Size = new Size(tableWidth, tableHeight);
    table.EndInit();          

    return table;           
}      

我从 cellWidth 数组评估单元格宽度,但所有列都以相同的宽度初始化。 如何根据需要设置单元格宽度?

最佳答案

试试这个:

int width = 40;
cell.SizeF = new SizeF(cell.SizeF.Width + width, cell.SizeF.Height);

关于c# - 如何在 C# 中动态分配运行时的表格单元格宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24081183/

相关文章:

excel - XtraReports 以编程方式设置 Excel 导入单元格格式

devexpress - 如何处理 Reports Devexpress 中标签的自动宽度属性?

c# - 在 MSTest 中忽略从通用基类继承的测试类

c# - 使用 C# 从数组中的单词中删除字符串中的单词

除英语之外的 C# WritePrivateProfileString() 值

c# - 必须是具有公共(public)无参数构造函数的非抽象类型

gridview - MVC Devexpress Gridview 列,如何向特定列添加多个设置?

c# - 将 SVG 图像添加到资源文件中

c# - devexpress 进度条 performstep 方法不显示