1

I'm using WPF and I have a DataGrid binded to an Object List with each column to each property:

<DataGrid x:Name="Docs" Margin="29,211,25,66" IsReadOnly="False" AutoGenerateColumns="False" 
              DataContext="{Binding Source={StaticResource Rutine}}" CanUserReorderColumns="False" 
              ItemsSource="{Binding Documents,Mode=TwoWay}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Document Type"  Binding="{Binding DocumentType}" />
        <DataGridTextColumn Header="Document (Name)" Binding="{Binding DocumentName}" /></DataGrid.Columns></DataGrid>


public IList<CSDoc> Documents
{
    get
    {
        return _Documents;
    }

    set
    {
        _Documents = value;
        NotifyPropertyChanged("Documents");
    }
}


public class CSDoc
{
    public string DocumentType{ get; set; }
    public string DocumentName{ get; set; }
}

I allow the user to change the data, so that it can later be exported to an xlsx file.
But I need a DataTable Object to do that.
Another problem is that I need the Headers as they are, because the xlsx file is later uploaded to another system that makes validations with spaces and parentheses.
Is there a way to create the DataTable from the DataGrid user view without cycling each cell?
And because of each Header, without the Object List being cycled as well...

Yael
  • 1,566
  • 3
  • 18
  • 25
Darknesstiller
  • 39
  • 1
  • 10

2 Answers2

0

Make your CSDoc implement INotifyPropertyChanged (to achieve TwoWay binding), and handle it in your ViewModel layer.

To convert the IList<CSDoc> to a DataTable, you can either do it manually if there are only two columns (faster), or using the generic approach outlined here: Convert generic List/Enumerable to DataTable?

Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
  • I did implement INPC on my Routine class, and my List works fine – Darknesstiller Feb 11 '16 at 22:35
  • I just thought it could be some way to take like the ViewState of the DataGrid... – Darknesstiller Feb 11 '16 at 22:35
  • 1
    It is certainly possible to do so directly from the DataGrid, but unless you are building a generic export function, I think this sort of logic should not depend on the UI. If you want to do it from the UI, then yes, you'd need to iterate over the Rows and Columns of the DataGrid since that is where the data is 'stored'. – Troels Larsen Feb 11 '16 at 22:44
0

I think This post Cover pretty well your question Convert generic List/Enumerable to DataTable? Or you can use the iterative way.

try
{
    var dataTable = new DataTable();

    dataTable.Columns.Add("DocumentType");
    dataTable.Columns.Add("DocumentName");

    foreach (CSDoc Myelement in Documents)
    {
        var newRow = dataTable.NewRow();

        // fill the properties into the cells
        newRow["DocumentType"] = element.DocumentType;
        newRow["DocumentName"] = element.DocumentName;

        dataTable.Rows.Add(newRow);
    }

    // export to an xlsx file
}
catch (Exception e)
{
    MessageBox.Show("error" + e.ToString());
}
Community
  • 1
  • 1
Mr Rubix
  • 1,492
  • 14
  • 27