我在 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/