Wednesday, September 9, 2009

Sort the Data Table by Column

public DataTable SortDataTableByColumn(DataTable dataTable, string sortColumnName, string sortOrder)

{

DataRow[] filteredRows;

if (!string.IsNullOrEmpty(sortColumnName))

{

if (string.IsNullOrEmpty(sortOrder))

sortOrder = "ASC";

filteredRows = dataTable.Select(string.Empty, sortColumnName + " " + sortOrder);

DataTable filteredDataTable = dataTable.Clone();

foreach (DataRow dataRow in filteredRows)

{

filteredDataTable.ImportRow(dataRow);

}

return filteredDataTable;

}

else

{

return dataTable;

}

}

No comments: