如何在C#中动态分配表格单元格宽度?

时间:2014-06-06 11:50:23

标签: 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数组中确定单元格宽度,但所有列都以相同的宽度进行初始化。 如何根据需要设置单元格宽度?

1 个答案:

答案 0 :(得分:0)

试试这个:

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