对dataGrid的内容进行分页,要保证当记录为空或记录数小于每页显示记录的设定值时,单击下“下一页”按钮时,不会执行下一页。请给出详细的例子。

解决方案 »

  1.   

    对dataGrid的内容进行分页,要保证当记录为空或记录数小于每页显示记录的设定值时,单击下“下一页”按钮时,不会执行下一页。请给出详细的例子。是在windows Form环境下
      

  2.   

    private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
    {
    try
    {
    if(e.Button == btnPreView)
    {
    dlgPreview.ShowDialog();//打印预览
    }
    if(e.Button == btnPrintSetup)
    {
    dlgPrintSetup.ShowDialog();//打印设置
    }
    if(e.Button == btnPrint)
    {
    if(dlgPrint.ShowDialog() == DialogResult.OK)
    {
    printCourseDoc.Print();//打印
    }
    }
    if(e.Button == btnSave)
    {
    SaveFileDlg();//保存
    }
    if(e.Button == btnClose)
    {
    this.Close();//关闭
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message, "打印出现错误",
    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    } protected void SaveFileDlg()
    {//这里保存文件的代码未完成
    } protected void SaveFile()
    {//这里保存文件的代码未完成
    } //从“打印设置PrintSetup”中获取相关参数,进行打印
    //这里有个问题:如果选择纸张大小为“自定义大小”,那么在PrintPreview的时候就会出错,为什么呢?
    private void printCourseDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    int Cols = 0;
    x = e.MarginBounds.Left;//纸张可写处的最左边
    y = e.MarginBounds.Top;//纸张可写处的最上端 IsFullPage(dgTable, PrintingPageNumber, PrintRecordNumber);   
          
    int columnCounts = 0;//定义DataGrid中的列数
    string[] colText = new string[columnCounts];//定义一个与DataGrid中的列数相同大小的数组,存放其中的字段名称
    string[] colID = new string[columnCounts];//定义一个存放字段属性值(如ID,TeacherName)等的数组,打印DataGrid中的数据时要用到
    int[] colSize = new int[columnCounts];//定义一个存放各个字段大小的数组 //取得DataGrid中的列数,以及各个列的名称,并赋给ColText数组
    foreach(DataGridTableStyle myGridStyle in dgClasses.TableStyles)
    {
    string[] headerText = new string[myGridStyle.GridColumnStyles.Count];
    string[] headerID = new string[myGridStyle.GridColumnStyles.Count];
    int[] columnSize = new int[myGridStyle.GridColumnStyles.Count]; //遍历dgClasses中的每个表所有的字段
    foreach(DataGridColumnStyle myColumnStyle in myGridStyle.GridColumnStyles)
    {
    headerText[columnCounts++] = myColumnStyle.HeaderText;//获得字段名称,如“学号”等
    headerID[columnCounts-1] = myColumnStyle.MappingName;//获取字段属性值,如“ID”等
    columnSize[columnCounts-1] = myColumnStyle.Width;
    }
    colText = headerText;//赋给前者
    colID = headerID;//赋给前者
    colSize = columnSize;//赋给前者
    }
       
    try
    {
    //将当前页分成基本的单元
    if(printCourseDoc.DefaultPageSettings.Landscape == true)
    {
    X_unit = e.MarginBounds.Height / (columnCounts + 5);//[横向打印时]除以“要打印的字段总数+5”,得到纵向间隔大小
    Y_unit = e.MarginBounds.Width / printLines;
    }
    else
    {
    X_unit = e.MarginBounds.Width / (columnCounts + 5);//[纵向打印]除以“要打印的字段总数+5”,得到横向间隔大小
    Y_unit = e.MarginBounds.Height / printLines;
    }
    }
    catch(Exception ex)
    {
    // Common.ShowErrorDetail(ex.Message);
    }
       
    //打印文档标题,自定义
    string strPrintTitle = dgClasses.CaptionText;//文档标题,这里为“课程列表”
    DrawPoint = new PointF(x + e.MarginBounds.Width/2 - 50, y );//设定标题的打印位置坐标(x,y)=(矩形最左边坐标+矩形的宽度的一半,矩形最上边的坐标)
    e.Graphics.DrawString(strPrintTitle, PrintFont("宋体", 20), DrawBrush, DrawPoint);//打印文档标题
    y += Y_unit * 2;//打印完标题,往下移动两个“Y_unit”单位 //打印列名
    for(Cols=0;Cols<columnCounts;Cols++)
    {
    if(Cols == 0)
    {
    DrawPoint.X = X_unit;//如果是第一列,则从X_unit开始
    }
    else
    {
    DrawPoint.X += colSize[Cols-1] + X_unit;//如果是第二个以后,则从每次往前移动一个单元格和前一个的字段大小
    }
    DrawPoint.Y = y;//设定当前列名的打印位置
    e.Graphics.DrawString(colText[Cols], PrintFont("宋体", 10), DrawBrush, DrawPoint);//打印所有列名
    } DrawPoint = new PointF(X_unit, y);
    DrawLine(DrawPoint, e);//在列名下面打印一条黑色直线
    y += Y_unit;//打印完全部列名,往下移动“Y_unit”个单位 
    //打印DataGrid中的数据
    while(PrintingLine<PageRecordNumber)
    {
    DataRow dgRow = dgTable.Rows[PrintRecordComplete];
    IsFullPage(dgTable, PrintingPageNumber, PrintRecordNumber);//判断最后一页的记录数是否可以打满一页,否则出错。 for(Cols=0;Cols<columnCounts;Cols++)
    {
    if(Cols == 0)
    {
    DrawPoint.X = X_unit;
    }
    else
    {
    DrawPoint.X += colSize[Cols-1] + X_unit;
    }
    DrawPoint.Y = y;
    e.Graphics.DrawString(dgRow[colID[Cols]].ToString(), PrintFont("宋体", 10), DrawBrush, DrawPoint);
    }
    y += Y_unit;//打印完一行,就往下移动“Y_unit”个单位
    PrintingLine += 1;//正在打印的行数+1
    PrintRecordComplete += 1;//已经打印的记录数+1 if(y > e.MarginBounds.Bottom)
    {
    DrawPoint.X = X_unit * 1;
    DrawPoint.Y = y;
    DrawLine(DrawPoint, e);//在每个页面的底尾打印一条黑色直线 e.HasMorePages = true;//如果已经到达纸张底部,则开始新页 PrintingPageNumber += 1;//正要打印的页数+1
    PageRecordNumber = 0;//将当前要打印的行数置0
    PrintingLine = 0;//将正在打印的行数置0
    return;
    }
    } e.HasMorePages = false;
    } //只调用一次,用于初始化
    private void printCourseDoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
    {
    dgTable = (DataTable)dgClasses.DataSource;
    ColsCount = dgTable.Columns.Count; //当前页是横向还是纵向打印,并计算当前页总共可以打印的行数
    if(printCourseDoc.DefaultPageSettings.Landscape == false)
    {
    printLines = printCourseDoc.DefaultPageSettings.PaperSize.Height / (PrintFont("宋体", 10).Height + 10);//纵向打印的情况
    }
    else
    {
    printLines = printCourseDoc.DefaultPageSettings.PaperSize.Width / (PrintFont("宋体", 10).Height + 10);//横向打印的情况
    }
    } //计算,余下的记录条数是否还可以在一页打印,不满一页时为假
    protected void IsFullPage(DataTable dgTable, int PrintingPageNumber, int PrintRecordNumber)
    {
    //计算,余下的记录条数是否还可以在一页打印,不满一页时为假
    if(dgTable.Rows.Count - PrintingPageNumber * PrintRecordNumber >= PrintRecordNumber)
    {
    PageRecordNumber = PrintRecordNumber;//余下的条数还可以打满一页,将“每页可打印条数”赋给“当前要打印的行数”
    }
    else
    {
    PageRecordNumber = (dgTable.Rows.Count - PrintingPageNumber * PrintRecordNumber) % PrintRecordNumber;
    }
    } //Draw a line with a black pen.
    protected void DrawLine(PointF point, System.Drawing.Printing.PrintPageEventArgs e)
    {
    Pen blackPen = new Pen(System.Drawing.Color.Black, 1);
    e.Graphics.DrawLine(blackPen, point.X, point.Y + PrintFont("宋体", 10).Height, point.X * ColsCount, point.Y + PrintFont("宋体", 10).Height);
    } //返回字体设置
    protected Font PrintFont(string font, int size)
    {
    return new Font(font,size);
    }
    }
    }