如何在Datetimepicker中将数据从给定日期提取到当前日期

时间:2011-06-01 02:47:00

标签: c# datetimepicker

我有很多文件夹,有很多不同日期的数据文件。现在,如果我从DatePicker中选择日期,我的代码也会提取数据并提取日期,并将日期作为名称保存到txt文件(例如YYYYMMDD.txt20110131.txt),我选择了来自我的DatePicker的日期。

我在这些文件中有超过2年的数据。每次我选择提取数据的日期时,我都很难这样做。

如果我从DatePickers中选择开始日期到当前日期,则数据将从开始日期提取到当前日期,并将文件名保存为每个日期。

有什么建议吗?

这是我的代码:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
    DateTime dt = dateTimePicker1.Value;
    txtSelectedDate.Text = dt.ToString("yyyyMMdd");
    selectedDate = txtSelectedDate.Text;
}



private void button2_Click(object sender, EventArgs e)
{
    string DayBgSpot = "E:\\Folder";

    string DayBgSpotDestination = "E:\\Folder1";

    int DT = int.Parse(txtSelectedDate.Text);
    FileReader Reader = new FileReader();
    FileReader Reader1 = new FileReader();
    Reader.OpenDirectory(DayBgSpot);
    Reader.ReadNaster();
    string path = DayBgSpotDestination + "\\" + txtSelectedDate.Text + ".txt";
    StreamWriter Strwriter = new StreamWriter(path);
    try
    {
        while (Reader.iMaRecordsLeft > 0)
        {
            string SecName = Reader.sMaSecName;
            string Symbol = Reader.sMaSecSymbol;
            Symbol = prefix + Symbol;
            int abc = 0;

            Reader.OpenSecurityByName(Reader.sMaSecName);
            if (Reader.iSeRecords > 0)
            {
                while (Reader.iSeRecordsLeft > 0)
                {
                    Reader.ReadDay();
                    float O = Reader.dSeo;
                    float H = Reader.dSeh;
                    float L = Reader.dSel;
                    float C = Reader.dSec;
                    double V = Reader.dSeV;
                    double OI = Reader.dSrest;
                    string T = Reader.iSeTime.ToString();
                    string D = Reader.iSeDate.ToString();
                    if (int.Parse(D) == DT)
                    {
                        string a = string.Concat(SecName, ",", Symbol, ",", D, ",", T, ",", O, ",", H, ",",  L, ",", C, ",", V, ",", OI);
                        if (SecName != "" && V != 0)
                        {
                            Strwriter.WriteLine(a);
                        }
                    }
                }
            }



            abc++;


            Reader.ReadNaster();
        }

        Reader.CloseDirectory();
        Strwriter.Close();
        Strwriter.Dispose();



    }
    catch
    {
    }


}

1 个答案:

答案 0 :(得分:1)

这是逻辑。 可以说,您已将datetimepicker用于开始日期并生成报告直至日期。

使用按钮点击代码中的循环

DateTime stdate = Datetimepicker1.value;
while (stdate <= DateTime.Now)
{
    txtSelectedDate.Text = stdate.ToString("yyyyMMdd");
    selectedDate = txtSelectedDate.Text;

/* Here use your existing code as it is */
............
.........



stdate = stdate.AddDays (1); // It will get next date till present

}