我正在开发的程序中有一部分需要根据在表单上选择的日期时间选择器控件中选择的开始和结束日期来过滤所有不必要的行,并仅保留必要的行。一个 .csv 文件包含以下所有日期作为示例:
3/10/2015 3/9/2015 3/6/2015 3/5/2015 3/4/2015 3/3/2015 3/2/2015 2/27/2015 2/26/2015 2/25/2015 2/24/2015 2/23/2015 2/20/2015
如果我的日期时间选择器已选择开始日期为2/18/2015,结束日期为3/2/2015,则我希望只有这些项目包含在我的数据表中。我已经有了一个反转数据行的方法,但我需要知道如何过滤小于或等于结束日期且大于或等于开始日期的值。我的计划是在一个单独的方法中进行过滤,在该方法中传递我从读取 .csv 文件创建的现有表格。
到目前为止,我的代码如下:
3/10/2015 3/9/2015 3/6/2015 3/5/2015 3/4/2015 3/3/2015 3/2/2015 2/27/2015 2/26/2015 2/25/2015 2/24/2015 2/23/2015 2/20/2015
如果我的日期时间选择器已选择开始日期为2/18/2015,结束日期为3/2/2015,则我希望只有这些项目包含在我的数据表中。我已经有了一个反转数据行的方法,但我需要知道如何过滤小于或等于结束日期且大于或等于开始日期的值。我的计划是在一个单独的方法中进行过滤,在该方法中传递我从读取 .csv 文件创建的现有表格。
到目前为止,我的代码如下:
public DataTable PopulateTable()
{
DataTable stockTable = new DataTable();
try
{
FileStream stream = new FileStream(BuildFilePath(), FileMode.Open);
stream.Close();
}
catch (Exception ex)
{
MessageBox.Show("Unable to open filepath");
}
StreamReader reader = new StreamReader(BuildFilePath());
string header = "";
string line = "";
char[] delimeter = { ',' };
string[] headerData;
string[] rowData;
//read the header and add the names to the columns of the data table
header = reader.ReadLine();
headerData = header.Split(delimeter);
for (int i = 0; i < 5; ++i)
{
if (i == 0)
{
stockTable.Columns.Add(new DataColumn(headerData[i], typeof(DateTime)));
}
else
stockTable.Columns.Add(new DataColumn(headerData[i], typeof(decimal)));
}
//read each line and populate the data table rows
while (reader.Peek() > -1)
{
line = reader.ReadLine();
rowData = line.Split(delimeter);
stockTable.Rows.Add(rowData[0], rowData[1], rowData[2], rowData[3], rowData[4]);
}
reader.Close();
//before the table is returned here call the FilterTable function
return ReverseRowsInDataTable(stockTable);
}
private DataTable ReverseRowsInDataTable(DataTable table)
{
DataTable reversedDataTable = table.Clone();
for (int i = table.Rows.Count - 1; i >= 0; --i)
{
reversedDataTable.ImportRow(table.Rows[i]);
}
return reversedDataTable;
}
private DataTable FilterTable(DataTable table)
{
DataTable filteredTable = new DataTable();
return filteredTable;
}