Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF DataGridRow not updating cell order after column sort
    primarykey
    data
    text
    <p>I have a DataGrid that after is populated I want to export it to Excel format. So far I'm able to do that. My problem comes when I try to sort the column order. The header is built according to the right order but the DataGridRows are not.</p> <p>The image shows the final result:</p> <p><img src="https://i.stack.imgur.com/PaWPr.jpg" alt="Column Sort"></p> <p>In this example I swapped the 'ID Equipa' column with the 'Tipo Entidade', however in the excel file (on the right) the row values continue as if no change ever happened while the header is updated nicely.</p> <p>Don't know if this helps, but my 'ExportToExcel' class is based on this project <a href="http://www.codeproject.com/Articles/120480/Export-to-Excel-Functionality-in-WPF-DataGrid" rel="nofollow noreferrer">ExportToExcel Project</a> but instead of using it's class identifier</p> <pre><code>public class ExportToExcel&lt;T, U&gt; where T : class where U : List&lt;T&gt; { // ... } </code></pre> <p>I created this one</p> <pre><code>public class ExportToExcel } public ExportToExcel(List&lt;DataGridColumn&gt; columns, List&lt;DataGridRow&gt; dataToFill) { // ... } } </code></pre> <p>I think the problem is in my 'dataToFill' argument, since it keeps it's default cell order and does not update after a column sort event.</p> <p>I don't understand why this is happening. I would really appreciate if someone could shed some light at this issue.</p> <p>Thanks</p> <p>EDIT:</p> <p>Following Sheridan's advice I'm posting some extra code.</p> <p>This is how I extract the DataGrid Rows</p> <pre><code>public IEnumerable&lt;DataGridRow&gt; GetDataGridRows() { var itemsSource = dgEntities.ItemsSource as IEnumerable; if (null == itemsSource) yield return null; foreach (var item in itemsSource) { var row = dgEntities.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (null != row) { if (dgEntities.SelectedItems.Count == 0) yield return row; else if (row.IsSelected) yield return row; } } } </code></pre> <p>And here's how I instaciate the ExportToExcel class</p> <pre><code>public void ExportToExcel() { if (dgEntities.ItemsSource != null) { try { BLL.ExportToExcel export = new ExportToExcel(dgEntities.Columns.ToList(), GetDataGridRows().ToList()); export.GenerateReport(); } catch (Exception e) { MessageBox.Show(e.ToString()); } } } </code></pre> <p>This next bit is my override of the original code from the CodeProject Project I mentioned earlier</p> <pre><code>private object[] CreateHeader() { // Create an array for the headers and add it to the // worksheet starting at cell A1. List&lt;object&gt; objHeaders = new List&lt;object&gt;(); foreach (DataGridColumn dgc in Columns) { if (dgc.Visibility == System.Windows.Visibility.Visible) objHeaders.Add(dgc.Header); else deletedColumns.Add(dgc.DisplayIndex); } var headerToAdd = objHeaders.ToArray(); AddExcelRows("A1", 1, headerToAdd.Length, headerToAdd); SetHeaderStyle(); return headerToAdd; } private void WriteData(object[] header) { object[,] objData = new object[DataToFill.Count, header.Length]; for (int j = 0; j &lt; DataToFill.Count; j++) { DataGridRow row = DataToFill[j]; int i = 0; for (int x = 0; x &lt; Columns.Count; x++) { if (!deletedColumns.Contains(x)) { DataGridCell cell = GetCell(row, j, x); if (cell != null &amp;&amp; cell.Content is TextBlock) { objData[j, i] = ((TextBlock)cell.Content).Text; i++; } } } } AddExcelRows("A2", DataToFill.Count, header.Length, objData); AutoFitColumns("A1", DataToFill.Count + 1, header.Length); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload