最近做个项目中需要打印DataGridView中数据,因为产品质量存在三六九等,因此如果正常打印的话,会出现一条信息很长,其他信息很短情况,为让打印更美观,节约成本,需要将某几行变为横向的。这次我介绍一下关键代码部分。
/// <summary>
/// 打印单元格(行转列)
/// </summary>
/// <param name="dataGridView">DataGridView表</param>
/// <param name="limtRow">每一页控制行数</param>
/// <param name="n">页数</param>
/// <param name="groupNumber">多个待转列为一组</param>
/// <param name="eachGroupNo">每一组多少列</param>
/// <param name="AluxityStartCol">其他列的起始列</param>
/// <param name="AluxityEndCol">其他列的终止列</param>
private static void DataGridViewCellVsList(DataGridView dataGridView, int limtRow, int n, int groupNumber, int eachGroupNo, int AluxityStartCol, int AluxityEndCol)
{
CellPrint_List.Clear();
try
{
int rowsCount = dataGridView.Rows.Count;
//最后一行是供输入的行时,不用读数据。
if (dataGridView.Rows[rowsCount - 1].IsNewRow)
rowsCount--;
//读取单元格数据
int PrintStartRowIndex = 0;
int PrintEndRowIndex =0;
for (int m = 0; m < n; m++)
{
PrintStartRowIndex = m * limtRow * groupNumber;
PrintEndRowIndex = PrintStartRowIndex + limtRow;
for (int rowIndex = PrintStartRowIndex; rowIndex < PrintEndRowIndex; rowIndex++)
{
for (int i = 0; i < groupNumber; i++)
{
for (int columnsIndex = 0; columnsIndex < eachGroupNo; columnsIndex++)//内循环两次
{
if (dataGridView.Columns[columnsIndex].Visible && rowIndex + i * limtRow < rowsCount)
{
DataGridViewCellPrintX CellPrint = new DataGridViewCellPrintX();//单元格属性实例化 CellPrint.FormattedValue = dataGridView.Rows[rowIndex + i * limtRow].Cells[columnsIndex].FormattedValue.ToString();
CellPrint.RowIndex = rowIndex;
CellPrint.ColumnIndex = columnsIndex;
CellPrint.Font = dataGridView.DefaultCellStyle.Font; //.Rows[rowIndex].Cells[columnsIndex].Style.Font;
System.Drawing.Color TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex + i * limtRow].Cells[columnsIndex].Style.BackColor)
TmpColor = dataGridView.Rows[rowIndex + i * limtRow].Cells[columnsIndex].Style.BackColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex + i * limtRow].DefaultCellStyle.BackColor)
TmpColor = dataGridView.Rows[rowIndex + i * limtRow].DefaultCellStyle.BackColor;
else
TmpColor = dataGridView.DefaultCellStyle.BackColor;
CellPrint.BackColor = TmpColor;
TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex + i * limtRow].Cells[columnsIndex].Style.ForeColor)
TmpColor = dataGridView.Rows[rowIndex + i * limtRow].Cells[columnsIndex].Style.ForeColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex + i * limtRow].DefaultCellStyle.ForeColor)
TmpColor = dataGridView.Rows[rowIndex + i * limtRow].DefaultCellStyle.ForeColor;
else
TmpColor = dataGridView.DefaultCellStyle.ForeColor;
CellPrint.ForeColor = TmpColor;
CellPrint.Width = 40;
CellPrint.Height = dataGridView.Rows[rowIndex + i * limtRow].Height;
CellPrint_List.Add(CellPrint);
}
else
{
if (dataGridView.Columns[columnsIndex].Visible && rowIndex + i * limtRow > rowsCount - 1)
{
DataGridViewCellPrintX CellPrint = new DataGridViewCellPrintX();
CellPrint.FormattedValue = "";
CellPrint.RowIndex = rowIndex;
CellPrint.ColumnIndex = columnsIndex;
CellPrint.Font = dataGridView.DefaultCellStyle.Font; //.Rows[rowIndex].Cells[columnsIndex].Style.Font;
System.Drawing.Color TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)
TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor)
TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor;
else
TmpColor = dataGridView.DefaultCellStyle.BackColor;
CellPrint.BackColor = TmpColor;
TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor)
TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor)
TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor;
else
TmpColor = dataGridView.DefaultCellStyle.ForeColor;
CellPrint.ForeColor = TmpColor;
CellPrint.Width = 40;
CellPrint.Height = dataGridView.Rows[rowIndex].Height;
CellPrint_List.Add(CellPrint);
}
else if (dataGridView.Columns[columnsIndex].Visible)
{
DataGridViewCellPrintX CellPrint = new DataGridViewCellPrintX();
CellPrint.FormattedValue = dataGridView.Rows[rowIndex].Cells[columnsIndex].FormattedValue.ToString();
CellPrint.RowIndex = rowIndex;
CellPrint.ColumnIndex = columnsIndex;
CellPrint.Font = dataGridView.DefaultCellStyle.Font; //.Rows[rowIndex].Cells[columnsIndex].Style.Font;
System.Drawing.Color TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor)
TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.BackColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor)
TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.BackColor;
else
TmpColor = dataGridView.DefaultCellStyle.BackColor;
CellPrint.BackColor = TmpColor;
TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor)
TmpColor = dataGridView.Rows[rowIndex].Cells[columnsIndex].Style.ForeColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor)
TmpColor = dataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor;
else
TmpColor = dataGridView.DefaultCellStyle.ForeColor;
CellPrint.ForeColor = TmpColor;
CellPrint.Width = 40;
CellPrint.Height = dataGridView.Rows[rowIndex].Height;
CellPrint_List.Add(CellPrint);
}
}
}
}
for (int columnsIndexT = AluxityStartCol; columnsIndexT <= AluxityEndCol; columnsIndexT++)
{
if (dataGridView.Columns[columnsIndexT].Visible)
{
DataGridViewCellPrintX CellPrint = new DataGridViewCellPrintX();
CellPrint.FormattedValue = dataGridView.Rows[rowIndex - m * (groupNumber-1) * limtRow].Cells[columnsIndexT].FormattedValue.ToString();
CellPrint.RowIndex = rowIndex;
CellPrint.ColumnIndex = columnsIndexT;
CellPrint.Font = dataGridView.DefaultCellStyle.Font; //.Rows[rowIndex].Cells[columnsIndex].Style.Font;
System.Drawing.Color TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].Cells[columnsIndexT].Style.BackColor)
TmpColor = dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].Cells[columnsIndexT].Style.BackColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].DefaultCellStyle.BackColor)
TmpColor = dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].DefaultCellStyle.BackColor;
else
TmpColor = dataGridView.DefaultCellStyle.BackColor;
CellPrint.BackColor = TmpColor;
TmpColor = System.Drawing.Color.Empty;
if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].Cells[columnsIndexT].Style.ForeColor)
TmpColor = dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].Cells[columnsIndexT].Style.ForeColor;
else if (System.Drawing.Color.Empty != dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].DefaultCellStyle.ForeColor)
TmpColor = dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].DefaultCellStyle.ForeColor;
else
TmpColor = dataGridView.DefaultCellStyle.ForeColor;
CellPrint.ForeColor = TmpColor;
CellPrint.Width = 40;
CellPrint.Height = dataGridView.Rows[rowIndex - m * (groupNumber - 1) * limtRow].Height;
CellPrint_List.Add(CellPrint);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "出错!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
转行后的效果如下:
说明以下:首列是0,共6组(编号,LOT色,数量),每组3列,其他列的起始列: 3,其他列的终止列:4;
希望对你有帮助!