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
    评论
注意:本3.0版控件过于老旧,该控件最新版本为4.7版,您可以到http://myyouping.download.csdn.net/免费下载,也是完全免费的,没有任何功能及使用时间限制,0积分下载。我将3.0版控件的下载分提为10分,是希望大家使用最新版的控件。 控件特色: 1、超强大的DataGridView打印功能,不光可以以多种形式(普通打印、分栏打印、跨页打印、工资条打印打印DataGridView表格,基本上能完全按DataGridView控件本身设置的格式如字体、字号、背景颜色、前景颜色、单元格对齐方式等打印出来,文字图像都可以打印,而且是完全根据表格当前的显示顺序进行打印的,如果您在使用时调整了列的顺序,刷新打印后就会按调整后的列显示顺序进行打印(这是网上很多DataGridView打印控件没有实现的),基本上做到了所见即所得的打印。 2、强大的文本打印输出功能,打印文本时,如果需要,控件会自动换行或换页打印输出。 3、支持同一文档多种版面格式打印(类似于Word中的节的功能):对同一份文档,不同的页面可以设置不同的格式(纸张大小、纸张方向、页边距),只需要在新增一页时在NewPage方法中指定要使用的页面格式即可,使用非常简单。 4、报表功能。本控件允许将当前打印预览的内容保存为报表文件,以后使用本控件重新打开该报表文件即可重现原来保存报表时的打印内容。 5、打印方案保存与读取功能。可以将当前打印参数保存为打印方案文件,或都从保存的打印方案文件中读取打印参数。 6、水印功能。根据需要,可以在页面中打印或不打印以半透明空心文字打印水印。 7、特殊文字效果功能。本版控件具有打印浮雕文字、阴影文字、空心文字、块文字的功能,效果非常不错。 8、页眉页脚中既可打印文字,也可打印图像,或者即打印图像又打印输出文字。 9、图像打印输出功能。 本控件包括两个打印组件:DGVprint打印组件与VB2008print打印组件。VB2008Print为通用打印控件(为可视化组件),可以混合打印一个或多个DataGridView表格、DataGrid表格与文字、图片等内容;而DGVprint是以VB2008Print为基础开发的(为非可视化组件),为专用的表格打印控件,可以很方便打印单个DatatGridview、DataGrid、DataTable表格的内容,并提供打印时的可视化设置界面,可设置表格打印方式(普通打印、分栏打印、跨页打印、工资条打印)、标题内容及字体、页眉页脚、选择要打印表格列、列顺序调整及列格式设置、更改列标题名称等,使用非常方便,功能非常强大。 与本控件的上一个版本2.1版相比,本版控件新增功能如下: VB2008Print打印组件新增功能: 1、多种特效文字打印输出功能,能打印的特效文字有 空心文字、浮雕文字、块文字、阴影文字等。 2、水印打印功能。可以在面页中以半透明空心文字的形式打印背景水印,只需要简单设置控件的WaterMarkText等几个以WaterMark开头的属性即可,程序会自动打印水印。 3、PrintDGV函数可直接使用DGVPrint组件保存的打印方案文件打印输出DataGridView表格, 4、在页眉页脚中不光可以打印文本,还可以打印图像,文本与图像可同时打印(即在图像上显示文本)。此外,页眉页脚文字可以换行打印了,页面的左边也右边距也可以打印内容了(调用相应的函数PrintLeft与PrintRight实现)。 5、改进DrawText函数输出文本的功能,现在,即便调用没有指定打印区域或打印宽度的DrawText函数输出文本,打印输出时控件也会智能换行和换页(原版本是需要指定打印宽度才能自动换行换页打印的) 6、改进DrawImage与DrawCellImage输出图像功能,如果图像比较小(小于打印区域大小),可以不进行放大打印。(但如果图像大于打印区域的话,还是采用整体缩小打印,而不是区域剪裁打印)。 7、增加IsShowPrintStatusDialog属性,指示在发送到打印打印时,是否显示一个指示正在打印的状态窗口(可以取消打印),为TRUE表示要显示,为False表示不显示。 8、改进页眉页脚事件,将原来的HeaderOut与FooterOut统一为HeaderFooterOut事件,在该事件中,您可以调用PrintFooter、PrintHeader、PrintLeft、PrintRight函数分别打印上下左右的页眉。(PrintLeft与PrintRight函数为新增加的,用于在左边与右边页边距处输出内容) DGVPrint打印组件新增功能: 1、打印方案保存与读取功能。本版控件可以将您的可视化设置(包括列格式设置等)全部保存为打印方案文件(文本文件,您可以用记事本打开并修改),并有读取方案文件的功能,不再需要每次都进行打印格式设置了,一劳永逸! 2、直接调用打印方案文件打印功能。您不光可以设计DGVPrint打印组件的属性来进行打印,还可以直接调用DGVPrint组件中保存的打印方案文件,直接利用保存的方案文件的参数进行打印预览输出。 3、新增在可视化打印参数设置界面的列标题重命名功能,可能修改列标题要打印的名字。 4、水印打印功能。如果水印文本设置为空,则不打印水印。 5、导出数据成Excel功能。暂未提供该功能的函数接口,只在打印参数设置窗口中增加了一个数据导出的按钮,可以将当前要打印DataGridView的内容导出成Excel文件。该功能以后会进一步完善。 6、在进行页眉页脚文本设置时,可以用 [页码] 代表要输出的当前页码,用 [总页数] 代表要输出总页数,控件在进行输出时,会自动将其转换为对应的页码与文档总页数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值