How to print only the selected grid rows

來源:互聯網
上載者:User

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 #1private 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 3private 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 #4private GridControl GetGridWithSelection(GridControl grid) {GridControl clonedGrid = CloneGrid(grid);DataTable clonedTable = GetTableOfSelectedRows(grid.MainView as GridView);clonedGrid.DataSource = clonedTable;return clonedGrid;}// step #5private void PrintGrid() {if(gridView1.SelectedRowsCount == 0) printableComponentLink1.Component = gridControl1;elseprintableComponentLink1.Component = GetGridWithSelection(gridControl1);printableComponentLink1.CreateDocument();printableComponentLink1.ShowPreview();}

[VB.NET]Open in popup window

Imports DevExpress.XtraGridImports DevExpress.XtraGrid.Views.Grid' step #1Private 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 resultTableEnd Function' steps ##2 and 3Private 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 resultGridEnd Function' step #4Private 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 clonedGridEnd Function' step #5Private 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.