CSV Reports
DataTableToCsvString(DataTable,List<String>) Method
Example 



KellermanSoftware.CsvReports Namespace > CsvWriter Class > DataTableToCsvString Method : DataTableToCsvString(DataTable,List<String>) Method
The table to output
The column names in order to output
Write the rows of a data table to a CSV string
Syntax
Public Overloads Function DataTableToCsvString( _
   ByVal table As DataTable, _
   ByVal columnNameOrder As List(Of String) _
) As String
Dim instance As CsvWriter
Dim table As DataTable
Dim columnNameOrder As List(Of String)
Dim value As String
 
value = instance.DataTableToCsvString(table, columnNameOrder)
public string DataTableToCsvString( 
   DataTable table,
   List<string> columnNameOrder
)
public:
String^ DataTableToCsvString( 
   DataTable^ table,
   List<String^>^ columnNameOrder
) 

Parameters

table
The table to output
columnNameOrder
The column names in order to output

Return Value

CSV String
Example
//Example Class
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDate { get; set; }
}
 
CsvWriter csvWriter = new CsvWriter(); //Trial Mode
//CsvWriter csvWriter = new CsvWriter("place user name here", "place license key here"); //License Mode
 
DataTable table = new DataTable();
table.Columns.Add("FirstName", typeof(string));
table.Columns.Add("LastName", typeof(string));
table.Columns.Add("BirthDate", typeof(string));
 
DataRow row = table.NewRow();
row["FirstName"] = "John";
row["LastName"] = "Smith";
row["BirthDate"] = DateTime.Now.AddDays(-1).ToString();
table.Rows.Add(row);
 
//Order the output by Last Name, First Name, Birth Date
List<string> columnNameOrder = new List<string>();
columnNameOrder.Add("LastName");
columnNameOrder.Add("FirstName");
columnNameOrder.Add("BirthDate");
 
string results = csvWriter.DataTableToCsvString(table, columnNameOrder);
 
Console.WriteLine(results);
'Example Class
Public Class Person
    Public Property FirstName() As String
    Public Property LastName() As String
    Public Property BirthDate() As Date
End Class
 
Dim csvWriter As New CsvWriter() 'Trial Mode
'Dim csvWriter As New CsvWriter("place user name here", "place license key here") 'License Mode
 
Dim table As New DataTable()
table.Columns.Add("FirstName", GetType(String))
table.Columns.Add("LastName", GetType(String))
table.Columns.Add("BirthDate", GetType(String))
 
Dim row As DataRow = table.NewRow()
row("FirstName") = "John"
row("LastName") = "Smith"
row("BirthDate") = Date.Now.AddDays(-1).ToString()
table.Rows.Add(row)
 
'Order the output by Last Name, First Name, Birth Date
Dim columnNameOrder As New List(Of String)()
columnNameOrder.Add("LastName")
columnNameOrder.Add("FirstName")
columnNameOrder.Add("BirthDate")
 
Dim results As String = csvWriter.DataTableToCsvString(table, columnNameOrder)
 
Console.WriteLine(results)
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

CsvWriter Class
CsvWriter Members
Overload List