Farhan Shariff

Farhan Shariff

  • NA
  • 933
  • 241.7k

Sorting Datatable

Mar 19 2014 5:32 AM
 I am using a text parser to import a .csv file to Datatable
I want to sort the table from smallest to largest.
There are negative values in the table so type conversion has to be considered how to I approach the problem.
I tried  table4.Columns.Add("datecolumn1",typeof(double)); it says A column named 'datecolumn1' already belongs to this DataTable.
 
private static DataTable GetDataTabletFromCSVFile3(string csv_file_path3)
{
DataTable table4 = new DataTable("Refrence");
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path3))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn1 = new DataColumn(column);
datecolumn1.AllowDBNull = true;
table4.Columns.Add(datecolumn1);
//filterExp = filterExp + datecolumn1 + " asc ,";
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData[i] == "")
{
fieldData[i] = null;
}
}
table4.Rows.Add(fieldData);
}

Answers (2)