DataGridView中列向数据横向打印

最近做个项目中需要打印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;

希望对你有帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值