Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
30
Get current Sortorder in XamGrid
posted

I'm using a XamGrid to present Productinfos. The User is able to sort most of the columns as he likes. For Printing, I'm creating a FlowDocument and loop throw the Datasource of the XamGrid to copy the Data into a table. It all works fine and I'm able to Print or Preview all Data like it should be. The only problem I'm not able to resolve is to print the Data in the current used Sortorder. By looping throuh the DataSource, all Records are in the original Order, like it was at the time loading the Data into the XamGrid.

Is there a way to loop through all Records in the current Sortorder, like it is showen on the Screen by the time I start the printing?

Here are two codesnipets for better understanding:

XAML-Code for the XamDataGrid

<controls:ExtendedGrid Grid.Row="3" Margin="0,0,11,0" x:Name="DGArtikel" Theme="Office2013" GroupByAreaLocation="None" DataSource="{Binding Artikel}"
                                   FontSize="14" FontWeight="SemiBold" Focusable="False" SelectedItemsChanged="DGArtikel_OnSelectedItemsChanged" ScrollViewer.VerticalScrollBarVisibility="Auto"
                                   ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <controls:ExtendedGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:TemplateField Column="0" AllowHiding="Never" Label="Vorschaubild" Name="ImageSource" Width="108" AllowSorting="False" AllowResize="False" >
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <Image Source="{Binding RelativeSource={RelativeSource AncestorType=igDP:CellValuePresenter}, Path=Record.DataItem.(wm:CMSWarenkorbArtikel.ImageSource)}" Height="60" Width="80"/>
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                    </igDP:TemplateField>
                    <igDP:TemplateField Column="1" AllowHiding="Never" Label="Artikel" Name="AusgabeArtikelText" Width="0.6*" >
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=igDP:CellValuePresenter}, Path=Record.DataItem.(wm:CMSWarenkorbArtikel.AusgabeArtikelText)}" FontSize="14" FontWeight="SemiBold" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="11,3,3,3" Foreground="Gray"/>
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                    </igDP:TemplateField>
                    <igDP:TemplateField Column="2" AllowHiding="Never" Label="Menge" Name="Anzahl" Width="0.06*" >
                        <igDP:Field.Settings>
                            <igDP:FieldSettings SortComparer="{StaticResource CustomNumericSort}" />
                        </igDP:Field.Settings>
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=igDP:CellValuePresenter}, Path=Record.DataItem.(wm:CMSWarenkorbArtikel.Anzahl)}" FontSize="14" FontWeight="SemiBold" HorizontalAlignment="Right" Margin="11,3,3,3" Foreground="Gray"/>
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                    </igDP:TemplateField>
                    <igDP:TemplateField Column="3" AllowHiding="Never" Label="Nettopreis" Name="AusgabeNettoEinzelpreis" Width="0.15*" AllowSorting="False" >
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=igDP:CellValuePresenter}, Path=Record.DataItem.(wm:CMSWarenkorbArtikel.AusgabeNettoEinzelpreis)}" FontSize="14" FontWeight="SemiBold" HorizontalAlignment="Right" TextWrapping="Wrap" Margin="11,3,3,3" Foreground="DarkGray"/>
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                    </igDP:TemplateField>
                    <igDP:TemplateField Column="4" AllowHiding="Never" Label="Summe" Name="GesamtpreisFormatiert" Width="0.15*">
                        <igDP:Field.Settings>
                            <igDP:FieldSettings SortComparer="{StaticResource CustomNumericSort}" />
                        </igDP:Field.Settings>
                        <igDP:TemplateField.DisplayTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=igDP:CellValuePresenter}, Path=Record.DataItem.(wm:CMSWarenkorbArtikel.GesamtpreisFormatiert)}" FontSize="14" FontWeight="SemiBold" HorizontalAlignment="Right" Margin="11,3,3,3" Foreground="DimGray"/>
                            </DataTemplate>
                        </igDP:TemplateField.DisplayTemplate>
                    </igDP:TemplateField>
                </igDP:FieldLayout>
            </controls:ExtendedGrid.FieldLayouts>
        </controls:ExtendedGrid>

And this is the codesnippet for creating the table in a FlowDocument for Printing:

//Get Data from DataSource
DataTable dataTable = CreateDataTable<CMSWarenkorbArtikel>((IEnumerable<CMSWarenkorbArtikel>)this.DGArtikel.DataSource);

//Loop  throuh  all  Records  of DataSource
            foreach (DataRow dataTableRow in dataTable.Rows)
            {
                TableRow tableRow = new TableRow();
                rowGroup.Rows.Add(tableRow);
                columnIndex = 0;
                foreach (DataColumn column in dataTable.Columns)
                {
                    if (!fieldLayout.Fields.Any(x => x.Name == column.ColumnName))
                    {
                        continue;
                    }

                    Paragraph paragraph;

                    if (columnIndex == 0 && !string.IsNullOrEmpty(dataTableRow[column].ToString()))
                    {
                        System.Windows.Controls.Image image = new System.Windows.Controls.Image
                        {
                            Source = new BitmapImage(new Uri(dataTableRow[column].ToString())),
                            Width = 80,
                            Height = 60,
                            Margin = new Thickness(11, 8, 0, 0),
                            HorizontalAlignment = HorizontalAlignment.Center,
                            VerticalAlignment = VerticalAlignment.Center
                        };

                        paragraph = new Paragraph(new InlineUIContainer(image));
                    }
                    else
                    {
                        TextBlock textBlock = new TextBlock
                        {
                            Text = dataTableRow[column].ToString(),
                            Margin = new Thickness(5, 5, 5, 0)
                        };
                        paragraph = new Paragraph(new InlineUIContainer(textBlock))
                        {
                            TextAlignment = alignments[columnIndex]
                        };
                    }

                    TableCell cell = new TableCell(paragraph)
                    {
                        BorderBrush = new SolidColorBrush(Colors.Black),
                        BorderThickness = new Thickness(0.5, 0.5, 0.5, 0.5),
                        LineHeight = 30
                    };
                    tableRow.Cells.Add(cell);

                    columnIndex++;
                }
            }

Parents
  • 2480
    Offline posted

    Hello Sandy,

    Thank you for the provided code-snippets!

    Based on them, I understand that you are using the XamDataGrid control rather than the XamGrid, is this correct?

    What I can say is that you can leverage the XamDataGrid’s Records collection that returns them according to the sort order. I believe you will find the Iterate through the Records Collection topic in our documentation very helpful on the matter. In particular, the DataRecords expose the underlying data item via the read-only DataItem property, which you can use to build your custom table in a similar to your current way. Instead of columns, the DataRecord also exposes a Cells collection.

    Please, check out the referenced documentation and API links and let me know of any other questions on the matter.

    Best regards,
    Bozhidara Pachilova
    Associate Software Developer

Reply Children