Hi,
I am using RecordSelectorNumberType="VisibleIndex" in the field settings to display record number in xamdatagrid. When I export to excel, I want to export the record number. How can I achieve this.
Thanks,
Lala
Hello Lala, In order to export the records' selectors in XamDataGrid to Excel, an approach I can suggest you is to handle the RecordExporting, HeaderAreaExporting and the ExportEnding events of the DataPresenterExcelExporter instance. Inside the RecordExporting and the HeaderAreaExporting events, we can create an indentation for the records and the labels by incrementing the CurrentColumnIndex in order to make space for the record selector values in Excel. This way prior to ending the exporting operation, by handling the ExportEnding event, we can manually assign respective values for the cells in Excel, that act as record selectors. void exporter_RecordExporting(object sender, RecordExportingEventArgs e){ e.CurrentColumnIndex++;} void exporter_HeaderAreaExporting(object sender, HeaderAreaExportingEventArgs e){ e.CurrentColumnIndex++;} void exporter_ExportEnding(object sender, ExportEndingEventArgs e){ for (int i = 1; i <= XDG.Records.Count; ++i) { e.CurrentWorksheet.GetCell("A" + (i + 1).ToString()).Value = i; }} I have prepared an application where this behavior has been implemented. If you require any further assistance on this matter, please do not hesitate to ask.
Accessing the xamdatagrid like you are doing (XDG) is not an option. I have view and view model and the way we are tieing up the view to viewmodel, the viewmodel can not access the xamdatagrid by name. the object sender in this event is DataPresenterExcelExporter. Is there any other way to do this? Also I am exporting heirarical data and I want to show the record number onlly on the top level records. Another complexity is I have the option to export selected records only,
Hello Lala, I have been looking into the behavior you have described and I can suggest you the following approach, which I have implemented in the attached and modified application: 1) In order not to reference the XamDataGrid directly, you can always reference the collection of items (bound to the grid) inside the ViewModel to get the count of records in the main FieldLayout.- vm.People.Count; 2) In order to set a record selector index for the top level records only, you can iterate through the "B" column cells of the CurrentWorksheet and whenever a non-empty cell is found, it means that it is a cell of a top level record and then we set a respective index value on the left of that cell. (same index, but column "A"). The iteration continues until our counter for currentTopRecordIndex becomes greater than the number of top level records. int TopRecordsCount = vm.People.Count; int currentTopRecordIndex = 1; int currentCellIndex = 2;while (currentTopRecordIndex <= TopRecordsCount){ // If a cell in "B" column is not empty, it contains a top record value. var cellValue = e.CurrentWorksheet.GetCell("B" + currentCellIndex).Value as string; if (!string.IsNullOrEmpty(cellValue)) { // Assigning a respective record selector index value for the top record on the left. e.CurrentWorksheet.GetCell("A" + currentCellIndex).Value = currentTopRecordIndex; ++currentTopRecordIndex; } ++currentCellIndex; } 3) In order to achieve the functionality from 2) and export only the selected top level records to excel, you can create a property in your ViewModel class, that is bound to the SelectedDataItems property of the XamDataGrid. By manually checking if the currently initialized record for exporting is a top level record and it is not selected in the InitializeRecord event of the exporter, we can skip the record along with it's descendants. By uncommenting the commented sections in the InitializeRecord and the ExportEnding event and commenting the top section in the ExportEnding event, you should be able to achieve the functionality in regards to exporting the selected top level records only. If you require any further assistance on this matter, please let me know.