最近做個項目中需要打印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;
希望對你有幫助!