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
785
Inserting Record Labels During an Excel Export
posted

Hello all,

I have a XamDataGrid in which I have re-templated the record selector to a custom-formatted TextBlock. When a row gets exported to Excel, I would like to inject the TextBlock's content as the first column. I've figured out how to make room for the extra column--in both HandleHeaderAreaExporting() and HandleRecordExporting() I increment args.CurrentColumnIndex by 1. However, I do not know how to place data into the empty column I've created. Can anybody offer any help in this area? Thanks!

Dave

 

  • 69686
    Suggested Answer
    posted

    Hello Dave,

    As you want to do this on a Record level, you can use the RecordExported event and set the values manually in the worksheet.

    In this event, you have the record, the record index, the worksheet, and everything that you need except the value of the Record Selector. You will not be able to get it directly, because you cannot access it from the visual tree.

    One way would be to create a property in the underlying data class, that will hold this value (will be bound to the record selector's text) and you can use the (e.Record as DataRecord).DataItem property to access it. Exporting should look like this:

    private void DataPresenterExcelExporter_RecordExported(object sender, RecordExportedEventArgs e)

            {

                e.Workbook.Worksheets[e.CurrentWorksheet.Index].Rows[e.CurrentRowIndex - 1].Cells[(e.Record as DataRecord).Cells.Count].Value = (e.Record as DataRecord).DataItem .... ;

            }

    e.CurrentRowIndex - 1 - because of the header record