Hi
Which is the best way to export xamlgrid to excell sheet?
Thanks
MK
Thanks Rumen, you helped point me in the right direction. This isn't eloquent or anything but it works.
All changes below are to the blog's XamDataGridExcelExporter.cs file.
I added a dictionary member to the class.private readonly Dictionary<int, ComboBoxItemsProvider> comboEditors = new Dictionary<int, ComboBoxItemsProvider>();
/// <summary>
/// Identifies the combo editors and saves their data source to the dictionary.
/// </summary>
/// <param name="xamDataGrid">The xam data grid.</param>
private void IdentifyComboEditors(DataPresenterBase xamDataGrid)
{
foreach (var fieldLayout in xamDataGrid.FieldLayouts)
foreach (var field in fieldLayout.Fields)
if (field == null || field.Settings == null || field.Settings.EditorStyle == null || field.Settings.EditorStyle.TargetType == null) continue;
if (field.Settings.EditorStyle.TargetType != typeof (XamComboEditor)) continue;
ComboBoxItemsProvider statusProvider = ((System.Windows.Setter)field.Settings.EditorStyle.Setters[0]).Value as ComboBoxItemsProvider;
comboEditors.Add(field.Index, statusProvider);
}
//Fill the Worksheet Cells with the values from the DataRecord Cells Subtracting 1 for the Relation
//_column if it is a normal DataRecord
if (record != null)
if (record.GetType() == typeof(DataRecord) && dataRecord != null)
for (int i = 0; i < dataRecord.Cells.Count; i++)
if (dataRecord.Cells[i].Field.IsExpandableResolved != true)
if(comboEditors.ContainsKey(i))
foreach (var item in comboEditors[i].Items)
Type classType = item.GetType();
PropertyInfo valueProperty = classType.GetProperty(comboEditors[i].ValuePath);
int itemValue;
int dataValue;
if (!int.TryParse(valueProperty.GetValue(item, null).ToString(), out itemValue) || dataRecord.Cells[i].Value == null || !int.TryParse(dataRecord.Cells[i].Value.ToString(), out dataValue))
continue;
if (itemValue != dataValue) continue;
PropertyInfo displayTextProperty = classType.GetProperty(comboEditors[i].DisplayMemberPath);
excelWorkSheetRow.Cells[i + level].Value = displayTextProperty.GetValue(item, null);
break;
else
excelWorkSheetRow.Cells[i + level].Value = dataRecord.Cells[i].Value;
I hope this make sense and helps people! I'm sure I could clean the code up a bit, but it's time to go home and the code is working. :)
Now that I re-read my post I didn't get all my thoughts onto the post, sorry for the confussion; I was lost in coding mode.
I'm exporting my xamDataGrid to excel. The provided code in the blog posting works great. The column I have the xamComboEditor in exports the data associated to the ValuePath that's in the DataSource. I would like the DisplayMember of the xamComboEditor to be exported instead. I'm new to wpf, so please feel free to correct my thoughts here: My thoughts on this would be to either gain direct access to the combo editor object and put the text into the excel cell or access the data source and perform a link query against for the display text. Thanks!
Hello,
Not completely sure how this is related to exporting, but maybe the technique described here would help:
http://forums.infragistics.com/forums/t/16052.aspxComboBoxItemsProvider statusProvider = xamDataGrid1.TryFindResource("StatusItemsProvider") as ComboBoxItemsProvider;
Or is there something in your scenario that we are missing?
I have a XamComboEditor as one of my column editors. The above example exports the selected value just fine. Anyone know or have a way of getting the displayed text from the xamComboEditor or maybe perform a linq query against its DataSource instead?
TIA!
Hi,
I would advise you to become familiar with a great solution, suggested by Andrew Flick - Exporting the XamDataGrid to Excel at
http://blogs.infragistics.com/blogs/andrew_flick/archive/2008/10/22/exporting-the-xamdatagrid-to-excel.aspx
The solution is presented through a working sample application.
Best Regards,Yanko