Convert List to DataTable

This post is related to the previous one - List instead of DataSet, with the ways how to convert DataTable to List.

Another side

However, it could be the case - when you need to do backfire - convert List to DataTable

For example, it could be useful when you are working with Syncfusion and want to export your list to Excel document. This Syncfusion framework does the method that will export a list to Excel. We just have:


  •  ImportArray
  •  ImportDataColumn
  •  ImportDataTable


But there are no methods that will get as input parameter some List and return to Excel in the result.

So we have created an extension method for List that allows us to convert List to DataTable. It is very useful. You can work in your solution with List. And just in the end when you want to export that List to Excel - you can call the method that will convert your List to DataTable. The code is very simple:




Сustom attributes

In this code we are using two custom attributes:


  •  ColumnOrderAttribute
  •  NotDataTableColumnAttribute



The first attribute we are using to show which number of column in DataTable will have current property of the list.

The second attribute we are using to show - do we need to include this column in DataTable or we want to skip this property and do not have it as DataTable column.




Not Data Table Column Attribute



And now we can use this ConvertToDataTable to code:


Use ConvertToDataTable

Use ConvertToDataTable


Best Regards. Andrew Tsopych CTO of Diceus

  • DxtMFQ5Z

    9/23/2017 8:21:23 AM |

    817811 766190You seem to be very professional in the way you write.::�~  211204