question

GarethWilliams-0328 avatar image
GarethWilliams-0328 asked ·

datagrid print and pagination

Is there a way to make the datagrid only display whole rows?
It can sometimes, depending on row height, show a partial row at the bottom of the grid.
This is fine when on screen - you can scroll - but when I am rendering this for printing this is a problem.

windows-uwp-runtime
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
FayWang-MSFT answered ·

Hello,

​Welcome to Microsoft Q&A!

There is no api can make the datagrid only display whole rows, it depends on the scrolling and row height. If you want to print dataGrid, you could use the PrintHelper class to print the DataGrid, but you still need to manually paging your data in code-behind. You can divide the long dataGrid to many small dataGrid and calculate the maximum number of dataGrid-Items that can display on the screen together and then put them into print-pages. Here is a similar thread, you can refer to it.


1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for getting back to me.
Looking at the example code it seems that the approach is to limit the number of rows added to each datagrid page.
The approach I have taken from this sample with some mods - which appears to work (though don't know if there are corner cases) - is to fix the row and column heights then determine how many rows can be added by simple math:
(int)((data_grid.ActualHeight - data_grid.ColumnHeaderHeight) / data_grid.RowHeight);
This means that it can adapt to page sizes better.
It appears to work for me with different pages from A4 down to smallest of pages, portrait and landscape, but as I say I don't know if this is truly the right approach for all cases.
This approach does mean I have to do an UpdateLayout() so I can determine the ActualHeight given to the datagrid before I can add rows.
Then another UpdateLayout afterwards once data is added - somewhat wasteful.

0 Votes 0 · ·