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

如何在C#中动态分配运行时的表单元格宽度?,c#,devexpress,xtrareport,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.Lo

我在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;           
}      
readonly int[]cellWidth={5,20,30,40,50,60};//{ 16, 100, 100, 30, 20, 16 };
专用XRTable CreateXRTableDetail(数据表dtAra)
{
XRTable table=新的XRTable();
table.BeginInit();
table.LocationFloat=新的DevExpress.Utils.PointFloat(0,5F);
table.Borders=BorderSide.All;
int tableHeight=0;
int tableWidth=0;
for(int i=-1;i<4;i++)
{
XRTableRow行=新的XRTableRow();
对于(int j=0;j<6;j++)
{
XRTableCell=新的XRTableCell();
单元格填充=1;
单位宽度=新单位(cellWidth[j],UnitType.Pixel);
cell.Width=(int)Width.Value;
细胞重量=1;
cell.TextAlignment=TextAlignment.MiddleCenter;
tableWidth+=单元格宽度;
if(i==-1)//头
{
行高=15;
cell.Text=dtAra.Columns[j].ColumnName;
cell.BackColor=Color.Gainsboro;
cell.Font=新字体(“tahoma”,6);
}
其他的
{
行高=40;
cell.Text=dtAra.Rows[i][j].ToString();
cell.Font=新字体(“tahoma”,5);
}
行.单元格.添加(单元格);
}
tableHeight+=行高;
table.Rows.Add(行);
}
tableWidth=tableWidth/table.Rows.Count;
table.Size=新尺寸(桌宽、桌高);
table.EndInit();
返回表;
}      
我正在从cellWidth数组中分配单元格宽度,但所有列都以相同的宽度初始化。 如何设置所需的单元格宽度?

试试以下方法:

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