We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

I've following issue in sfdatagrid winforms.

  1. Say I have a sfdatagrid with checkbox column and I want to export data rows to PDF but only the data rows which checkbox column data are checked then how can I achieve this??
  2. Adding page-count in footer of pdf pages not working.
  3. Margin editing in pdf pages not working.
Thank You



3 Replies

SD Swathi Dhatchanamoorthi Syncfusion Team May 1, 2023 06:01 PM UTC

Hi Urvik,


Query

Response

Say I have a sfdatagrid with checkbox column and I want to export data rows to PDF but only the data rows which checkbox column data are checked then how can I achieve this??

 

We have provided the workaround for your requirement for DataGrid Exporting PDF only for the row which has the check box enabled can be achieved by the following code snippet.

private void Pdf_Exporting(object sender, EventArgs e)

        {

            ObservableCollection<Object> Checkedlist= new ObservableCollection<object>();

            var list = (this.sfDataGrid.DataSource as ObservableCollection<OrderInfo>);

            foreach (var Item in list)

            {

                if(Item.IsShipped)

                    Checkedlist.Add(Item);

            }

            if (Checkedlist.Count > 0)

            {

                var document = sfDataGrid.ExportToPdf(Checkedlist, GetPdfExportOption());

 

                SaveFileDialog sfd = new SaveFileDialog

                {

                    Filter = "PDF Files(*.pdf)|*.pdf",

                    FileName = "document1"

                };

 

                if (sfd.ShowDialog() == DialogResult.OK)

                {

                    using (Stream stream = sfd.OpenFile())

                        document.Save(stream);

 

                    if (MessageBox.Show("Do you want to view the created Pdf file?", "Pdf has been created", MessageBoxButtons.OKCancel) == DialogResult.OK)

                        Open(sfd.FileName);

                }

            }

            else

            {

                MessageBox.Show("No items has been selected");

            }

        }

 

We have prepared the sample based on your requirement. Please find it in the attachment.

Adding page-count in footer of pdf pages not working.

 

We are currently checking your reported issue with provided information and we need two more business days to validate this. We will update you with further details on May 03, 2023.

Margin editing in pdf pages not working.

 

We are currently checking your reported issue with provided information and we need two more business days to validate this. We will update you with further details on May 03, 2023.


Attachment: PdfExporting_82c6941e.zip


UR Urvik replied to Swathi Dhatchanamoorthi May 2, 2023 12:51 PM UTC

Can you provide me following code in VB.Net Winforms???
I am facing some difficulties converting the code to VB.NET Winforms..
Also in the code you wrote a viewModel and added rows to collection.
Can you give me a simple example of it please? I have a datatable as datasource to the sfdatagrid.
Isn't there any simple way to do this?

Thanks



DM Dhanasekar Mohanraj Syncfusion Team May 3, 2023 12:37 PM UTC

Urvik,

Please find the response to your queries below,

Queries

Response

 

Can you provide me following code in VB.Net Winforms???

I am facing some difficulties converting the code to VB.NET Winforms..

Also in the code you wrote a viewModel and added rows to collection.

Can you give me a simple example of it please? I have a datatable as datasource to the sfdatagrid.

Isn't there any simple way to do this?

 

 

Here, we have prepared the demo with the data table with VB as you requested, please have a look at this.


Adding page-count in footer of pdf pages not working.

 

You can achieve your requirement to “Add page numbers in the header or footer of the exported PDF document” by using the dynamic fields: PdfPageNumberField and PdfCompositeField as shown below,

 

Dim options As New PdfExportingOptions()

options.RepeatHeaders = True

options.ExportGroupSummary = True

options.ExportFormat = True

options.FitAllColumnsInOnePage = False

options.ExportTableSummary = True

 

'Set document information.

Dim document As New Syncfusion.Pdf.PdfDocument()

 

Dim page = document.Pages.Add()

 

Dim bounds As New RectangleF(0, 10, document.Pages(0).GetClientSize().Width, 50)

Dim footer As New PdfPageTemplateElement(bounds)

Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 7)

Dim brush As PdfBrush = New PdfSolidBrush(Color.Black)

 

'Create page number field.

Dim pageNumber As New PdfPageNumberField(font, brush)

 

'Add the fields in composite fields.

Dim compositeField As New PdfCompositeField(font, brush, "{0}", pageNumber)

compositeField.Bounds = footer.Bounds

 

'Draw the composite field in the footer.

compositeField.Draw(footer.Graphics, New PointF(250, 40))

 

'Add the footer template at the bottom.

document.Template.Bottom = footer

Dim pdfGrid = sfDataGrid1.ExportToPdfGrid(sfDataGrid1.View, options)

pdfGrid.Draw(page, New PointF())


For more information related to this, please refer to the below knowledge base documentation link,

 

KB Link:  https://support.syncfusion.com/kb/article/8054/how-to-add-page-numbers-to-the-exported-pdf-document-in-winforms-datagrid-sfdatagrid

 

Margin editing in pdf pages not working.

 

 

Your requirement to “Setting the margin to the exported PDF” will be achievable by using the below code snippet,

Private Sub ExportClick(ByVal sender As Object, ByVal e As EventArgs)

       Dim Checkedlist As New ObservableCollection(Of Object)()

 

       For Each record In sfDataGrid1.View.Records.ToList()

             'Here typecast based on DataRowView to get data               

             Dim row = (TryCast((TryCast(record, RecordEntry)).Data, DataRowView)).Row

             If row("IsShipped").ToString() = "True" Then

                    Checkedlist.Add(row)

             End If

       Next record

       If Checkedlist.Count > 0 Then

             ' Create a new PDF document.

             Dim document = New PdfDocument()

 

             ' Setting the margins for the document.

              document.PageSettings.Margins.All = 70

 

             ' Adding a new page to the document.

             Dim page = document.Pages.Add()

 

             Dim options As New PdfExportingOptions()

 

             ' Exporting the pdf grid.

             Dim PDFGrid = sfDataGrid1.ExportToPdfGrid(Checkedlist, options)

 

             ' Draw the exorted grid into the created pages with the applied margin settings.

             PDFGrid.Draw(page, New PointF())

 

             Dim sfd As SaveFileDialog = New SaveFileDialog With {

                    .Filter = "PDF Files(*.pdf)|*.pdf",

                    .FileName = "document1"

             }

 

             If sfd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

                    Using stream As Stream = sfd.OpenFile()

                           document.Save(stream)

                    End Using

                    If MessageBox.Show("Do you want to view the created Pdf file?", "Pdf has been created", MessageBoxButtons.OKCancel) = System.Windows.Forms.DialogResult.OK Then

                          Open(sfd.FileName)

                    End If

             End If

       Else

             MessageBox.Show("No items has been selected")

       End If

End Sub


Here we have prepared the sample based on the above suggestion, please have a look at this.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: VB_9d792f4d.zip

Loader.
Up arrow icon