zoukankan      html  css  js  c++  java
  • How to print only the selected grid rows

    XtraGrid, XtraPrinting Library, eXpress Persistent Objects

    This property The XtraGrid control provides the GridView.OptionsPrint.PrintSelectedRowsOnly property which allows to accomplish this task. This property is available since version 2009 vol 1.0. ( Print (export) only selected rows and their details )

    With earlier versions of the XtraGrid control you can use the following solution:
    1. Create a filtered data table, which contains only those rows, which are selected in the grid.
    2. Programmatically create a new grid control.
    3. Assign the settings of the source grid to the dynamically created grid.
    4. Bind the dynamically created grid to the filterd data.
    5. Print the dynamically created grid via the PrintingSystem component.

    	

    [C#]Open in popup window

    using System.Data; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; // step #1 private DataTable GetTableOfSelectedRows(GridView view) { DataTable resultTable = new DataTable(); DataTable sourceTable = null; if(view.DataSource is DataView) sourceTable = ((DataView)view.DataSource).Table; else if(view.DataSource is BindingSource) { object dv = ((BindingSource)view.DataSource).List; sourceTable = ((DataView)dv).Table; } if(sourceTable != null) { resultTable = sourceTable.Clone(); foreach(int rowHandle in view.GetSelectedRows()) { DataRow row = view.GetDataRow(rowHandle); resultTable.Rows.Add(row.ItemArray); } resultTable.AcceptChanges(); } return resultTable; } // steps ##2 and 3 private GridControl CloneGrid(GridControl sourceGrid) { GridControl resultGrid = new GridControl(); resultGrid.MainView = new GridView(resultGrid); resultGrid.MainView.Assign(sourceGrid.MainView, false); Controls.Add(resultGrid); resultGrid.Visible = false; return resultGrid; } // step #4 private GridControl GetGridWithSelection(GridControl grid) { GridControl clonedGrid = CloneGrid(grid); DataTable clonedTable = GetTableOfSelectedRows(grid.MainView as GridView); clonedGrid.DataSource = clonedTable; return clonedGrid; } // step #5 private void PrintGrid() { if(gridView1.SelectedRowsCount == 0) printableComponentLink1.Component = gridControl1; else printableComponentLink1.Component = GetGridWithSelection(gridControl1); printableComponentLink1.CreateDocument(); printableComponentLink1.ShowPreview(); }
    	

    [VB.NET]Open in popup window

    Imports DevExpress.XtraGrid Imports DevExpress.XtraGrid.Views.Grid ' step #1 Private Function GetTableOfSelectedRows(ByVal view As GridView) As DataTable Dim resultTable As New DataTable If TypeOf view.DataSource Is DataView Then Dim sourceTable As DataTable = CType(view.DataSource, DataView).Table resultTable = sourceTable.Clone() Dim rowHandle As Integer For Each rowHandle In view.GetSelectedRows() Dim row As DataRow = view.GetDataRow(rowHandle) resultTable.Rows.Add(row.ItemArray) Next rowHandle resultTable.AcceptChanges() End If Return resultTable End Function ' steps ##2 and 3 Private Function CloneGrid(ByVal sourceGrid As GridControl) As GridControl Dim resultGrid As New GridControl resultGrid.MainView = New GridView(resultGrid) resultGrid.MainView.Assign(sourceGrid.MainView, False) Controls.Add(resultGrid) resultGrid.Visible = False Return resultGrid End Function ' step #4 Private Function GetGridWithSelection(ByVal grid As GridControl) As GridControl Dim clonedGrid As GridControl = CloneGrid(grid) Dim clonedTable As DataTable = GetTableOfSelectedRows(grid.MainView) ' clonedGrid.DataSource = clonedTable Return clonedGrid End Function ' step #5 Private Sub PrintGrid() If GridView1.SelectedRowsCount = 0 Then PrintableComponentLink1.Component = GridControl1 Else PrintableComponentLink1.Component = GetGridWithSelection(GridControl1) End If PrintableComponentLink1.CreateDocument() PrintableComponentLink1.ShowPreview() End Sub

    See Also:
    Printing the XtraGrid in the online documentation

    E-MAIL:yiwuya@hotmail.com
    MSN:yiwuya@hotmail.com
    QQ:304899972
    纺织软件
  • 相关阅读:
    简单的MsChart使用与遇到的麻烦
    SQLServer中case when 与列拼接
    关于集成单点登录问题
    IIS部署网站后,只有本服务器才能登录
    获取本周的周一日期与本周的周日日期
    34个漂亮的应用程序后台管理系统界面(系列二)
    2011年最佳免费 PSD 用户界面素材揭晓
    编程你使用快捷键了吗?
    汉字转全拼音函数优化方案(SQLServer),值得你看看
    WinForm企业应用框架设计【四】动态创建业务窗体
  • 原文地址:https://www.cnblogs.com/yiwuya/p/3018800.html
Copyright © 2011-2022 走看看