2
votes

I have a Datagrid formed by binding observablecollection, Now i want to provide Export to excel Functiom, So i need to Convert by datagrid to dataset or dataview.

public ObservableCollection<DataSourceVM> Backends { get; set; }
private void StartExport(String filepath)
   {
       try
     {
      DataTable bs = _dataGrid.ItemsSource as DataTable;
      _dataSet = bs.DataSet as DataSet;

   }
   catch (Exception e1)
  {
     MessageBox.Show("error");
  }
}

The ItemSource Contains a baseclass and Backend entity class

2

2 Answers

4
votes

You need to create the datatable by hand with the columns you need. I assume that the columns are the properties of DataSourceVM class.

public ObservableCollection<DataSourceVM> Backends { get; set; }

private void StartExport(String filepath)
{
    try
    {
        var dataSet = new DataSet();
        var dataTable = new DataTable();
        dataSet.Tables.Add(dataTable);

        // we assume that the properties of DataSourceVM are the columns of the table
        // you can also provide the type via the second parameter
        dataTable.Columns.Add("Property1");
        dataTable.Columns.Add("Property2");

        foreach (var element in Backends)
        {
            var newRow = dataTable.NewRow();

            // fill the properties into the cells
            newRow["Property1"] = element.Property1;
            newRow["Property2"] = element.Property2;

            dataTable.Rows.Add(newRow);
        }

        // Do excel export
    }
    catch (Exception e1)
    {
        MessageBox.Show("error");
    }
}
0
votes

You can also do it generically without knowing the type. I added the Attribute ExcelExportAttribute, so only properties with this Attribute are selected. You can remove the where-clause if you want all properties of the generic type.

    public static DataTable ToDataTable<T>(this IEnumerable<T> rows)
    {
        var result = new DataTable();
        // get generic properties that are marked with ExcelExportAttribute
        var propertyList = typeof(T).GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(ExcelExportAttribute))).ToList();
        // add generic columns
        foreach (var tProperty in propertyList)
        {
            result.Columns.Add(new DataColumn(tProperty.Name, Nullable.GetUnderlyingType(tProperty.PropertyType) ?? tProperty.PropertyType));
        }
        // add all rows to datatable
        foreach (var row in rows)
        {
            var dataRow = result.NewRow();
            // fill all columns of current row
            foreach (var tProperty in propertyList)
            {
                var type = (Nullable.GetUnderlyingType(tProperty.PropertyType));
                var defaultValue = type?.IsValueType == true ? Activator.CreateInstance(type) : default(T);
                dataRow[tProperty.Name] = typeof(T).GetProperty(tProperty.Name).GetValue(row) ?? defaultValue;
            }
            result.Rows.Add(dataRow);
        }
        return result;
    }