Use a DataView to Print the Contents of a Sorted Gridby Les Smith and Brian Davis |
How can I print the contents of a DataGrid, which has been sorted by the user? The DataView.Table returns the original, unsorted table, and does not reflect any sorting done by the user.
If you have a need to print or simply access the data from a DataGrid, after the user has sorted on one or more columns, you can do that by following the code in this article. There are several ways to accomplish this, but this seems to be the simpliest method that we have found.
First, recognize that you must use a DataView as the DataSource for the DataGrid. Second, you must also understand that you can not simply reference the DataView.Table after the DataGrid has been sorted. If you do that it returned DataTable will be the original DataTable from which the DataView was derived and will not reflect the order of the data in the sorted DataGrid.
So, to populate the grid, follow the code shown below. I have a form, with a DataGrid and other controls, from which the user can select the data to be retrieved from the database. I also have a Print button. To fill the DataGrid, I follow several steps. First, I retrieve a DataTable of the desired results from the database. Next, I call my CGrid Class to add TableStyles to the DataGrid for formatting purposes. Finally, I use the following code to bind the DataTable to the DataGrid.
If you have a need to print or simply access the data from a DataGrid, after the user has sorted on one or more columns, you can do that by following the code in this article. There are several ways to accomplish this, but this seems to be the simpliest method that we have found.
First, recognize that you must use a DataView as the DataSource for the DataGrid. Second, you must also understand that you can not simply reference the DataView.Table after the DataGrid has been sorted. If you do that it returned DataTable will be the original DataTable from which the DataView was derived and will not reflect the order of the data in the sorted DataGrid.
So, to populate the grid, follow the code shown below. I have a form, with a DataGrid and other controls, from which the user can select the data to be retrieved from the database. I also have a Print button. To fill the DataGrid, I follow several steps. First, I retrieve a DataTable of the desired results from the database. Next, I call my CGrid Class to add TableStyles to the DataGrid for formatting purposes. Finally, I use the following code to bind the DataTable to the DataGrid.
dv = dt.DefaultView dgTransRouting.SetDataBinding(dv, "") |
The grid will now be populated and display the data to the user. If the user should click on the column header of one or more columns, the data will be sorted in the DataGrid, according the last column header clicked.
Next, the user may click the Print button on the form and will expect to see the data on the resulting report be in the same sequence that they saw in the DataGrid. Since I am using a Print Class, CPrintReportString forVBNET or C#, I will not illustrate the code for printing here. The code shown below will be used in the Print Button Click Event. This code simply gets the DataView from the CurrencyManager of the DataGrid and passes the DataView to the Print method of the Class supporting this form.
Private Sub btnPrint_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPrint.Click Dim cm As CurrencyManager = _ CType(MeBindingContext(dgTransRouting.DataSource, _ dgTransRouting.DataMember), _ CurrencyManager) Dim dvSorted As DataView = CType(cm.List, DataView) tr.Print(dvSorted, cbFTP.Text, Me.dtpStTime.Value) End Sub |
The following code is a subset of the Print Method of the Class that supports the DataGrid form described earlier. The object of this method is to fill a StringBuilder with the data for the desired report. This is done by reading through the DataRows of the DataView's DataTable. I will show only the code that is necessary to illustrate accessing the sorted DataView's rows.
Public Sub Print(ByVal dv As DataView, _ ByVal stDate As Date) ' all code has been removed except that which is required for ' pertinent illustration for brevity's sake. For Each dr As DataRow In dv.Table.Select("", dv.Sort) ' here we can access the fields of the datarow dr sb.Append(MNI(dr("Department")).ToString.PadRight(6)) Next ' here I would normally have the code for calling the ' print class to do the actual printing of the report End Sub |
ليست هناك تعليقات:
إرسال تعليق