【问题标题】:Cell border in table表格中的单元格边框
【发布时间】:2011-11-14 10:09:39
【问题描述】:

我有一张正在打印的表格。我为所有单元格添加了边框,但是当表格进入/退出新页面时出现问题。然后,根据每个单元格占用的可用空间,将边框移动/停留在页面上。是否可以使该行依赖于 sted?

这是问题的图片

灰线是换页。

这是我的做法

foreach (Task task in TasksToShow)
        {
            myTable.RowGroups[0].Rows.Add(new TableRow());
            currentRow = myTable.RowGroups[0].Rows[rowCount++];

            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.TaskID.ToString()))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.TaskName))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.TaskResponsible))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.TaskResponsibleDepartment))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.Category))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.Status))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.Priority.ToString()))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.StartDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.ActualHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.EstimatedHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.EstimatedDeploymentDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.DesiredImplementationDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.APP.StartDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.APP.EstimatedHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.APP.ActualHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.IN.StartDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.IN.EstimatedHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.IN.ActualHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.SIS.StartDate.ToString("dd/MM/yy")))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.SIS.EstimatedHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.SIS.ActualHours.TotalHours.ToString() + "h"))));
            currentRow.Cells.Add(new TableCell(new Paragraph(new Run(task.Tags))));

            currentRow.FontSize = 10;
            currentRow.FontWeight = FontWeights.Normal;

            for (int n = 0; n < currentRow.Cells.Count; n++ )
            {
                currentRow.Cells[n].BorderThickness = new Thickness(1, 1, 1, 1);
                currentRow.Cells[n].BorderBrush = Brushes.Black;
            }
        }

还有没有办法让单元格的宽度取决于最大的单元格,而不仅仅是让所有单元格的宽度相同?

希望你能帮忙。

【问题讨论】:

  • 天哪。 1 个字,重构!
  • 什么意思?我只是在这里添加数据。我看不出如何让它变得更好!?
  • 怎么样:void AddRow(data) { currentRow.Cells.Add(new TableCell(new Paragraph(new Run(data)))); }
  • 是的,你是对的,这将使维护更容易......但这仍然不能回答我的问题。我现在将进行一些重构。

标签: c# printing tabular flowdocument


【解决方案1】:

我已经找到了一个解决方案,至少让它看起来一直都是一样的,但我对它还不完全满意。我刚刚将最后一个 for 循环中的厚度更改为:

for (int n = 0; n < currentRow.Cells.Count; n++ ) 
{ 
    currentRow.Cells[n].BorderThickness = new Thickness(0, 2, 1, 0); 
    currentRow.Cells[n].BorderBrush = Brushes.Black; 
}

【讨论】:

    【解决方案2】:

    最好为所有单元格表格添加边框。如果表格没有边框,则表格边框处的单元格边框只有网格线宽度的一半。

    同一行上两个单元格之间的网格线宽度由左侧单元格的单元格边框宽度加上右侧单元格的单元格边框宽度组成。如果单元格没有邻居,则网格宽度只有其他网格线的一半。为表格添加边框会添加不存在的邻居的边框。

    table.BorderBrush = Brushes.Black,
    table.BorderThickness = new Thickness(.5)
    
    cell.BorderBrush = Brushes.Black,
    cell.BorderThickness = new Thickness(.5)
    

    我知道,这并不能解决你的问题,但至少网格线看起来更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多