根据从日历视图中选择的数据从文件加载数据?

时间:2016-05-11 15:06:25

标签: c#

目前我有一个程序将加载一个包含数据的文件,然后将所有这些数据输出到适当列的表中。我想知道的是,是否可以让用户在日历视图上选择特定日期,然后它只会选择包含该数据的数据。这是我用于将数据加载到表中的代码:

                private void button1_Click(object sender, EventArgs e)
    {
        {



            {


                //List<string> colHeaders = new List<string>();

                var lines = File.ReadAllLines("F:\\data.hrm").SkipWhile(f => !f.StartsWith("[HRData]")).Skip(1);
                foreach (var line in lines)
                    dataGridView1.Rows.Add(line.Split(
                        new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries));
            }

            int total = dataGridView1.Rows.Cast<DataGridViewRow>()
            .Sum(t => Convert.ToInt32(t.Cells[0].Value));




        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int speed = 0;
        int power = 0;
        int hrate = 0;
        int altitude = 0;


        for (int i = 0; i < dataGridView1.Rows.Count; ++i)
        {
            speed += Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
            power += Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value);
            hrate += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
            altitude += Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);


            int count_row = dataGridView1.Rows.Count;
            double avgspeed = speed / count_row;
                            double avgpower = power / count_row;

                            double avghrate = hrate / count_row;
            double avgaltitude = altitude / count_row;


                            txt.Text = "Your average speed was: " + avgspeed.ToString() + "\r" + "your average power was : " + avgpower.ToString() + "\r" + "Your average heart rate was : " + avghrate.ToString() + "\r" + "Your average altitude was : " + avgaltitude.ToString() + "\r";

        }

        var Maxspeed = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .Max(r => Convert.ToInt32(r.Cells["speed"].Value));


        txt.Text = txt.Text + "Your max speed was: " + Maxspeed + "\r";

        var Maxpower = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .Max(r => Convert.ToInt32(r.Cells["watts"].Value));

        txt.Text = txt.Text + "Your max power was: " + Maxpower + "\r";


        var Maxhrate = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .Max(r => Convert.ToInt32(r.Cells["HeartRate"].Value));

        txt.Text = txt.Text + "Your max heart rate was: " + Maxhrate + "\r";


        var MaxAltitude = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .Max(r => Convert.ToInt32(r.Cells["altitude"].Value));

        txt.Text = txt.Text + "Your max altitude was: " + MaxAltitude + "\r";

        var minhrate = 777;
         minhrate = dataGridView1.Rows.Cast<DataGridViewRow>()
                    .Min(r => Convert.ToInt32(r.Cells["HeartRate"].Value));

        txt.Text = txt.Text + "Your min heart rate was: " + minhrate + "\r";



        }


    }
}

以下是数据本身的片段,实际上正在读入表单:

[Params]
Version=106
Monitor=34
SMode=111111100
Date=20130205
StartTime=15:46:20.0
Length=01:06:18.9


//n header 
[HRData]
91  154 70  309 83  6451
91  154 70  309 83  6451
92  160 75  309 87  5687
94  173 80  309 87  5687

1 个答案:

答案 0 :(得分:0)

好的,我不会反过来反序列化数据,但是你需要两个类:

1)Params(或其他)包含这些

的属性
  • 版本
  • 监控
  • SMode
  • 日期
  • StartTime
  • 长度
  • 阅读 - 将阅读[]

2)阅读   - 速度   - 力量  - 心率   - 海拔高度

然后你需要解析你的文件,并生成一个Params类数组,同时&#34; inside&#34;每个参数,生成每个阅读的数组并嵌套它。

我不会为你写这个,但是一旦你拥有了那个数组,你应该能够用LINQ过滤它们。

您一路上需要的功能:

DateTime.Parse,也许是DateTime.ParseExact T []。当 T []。康卡特 - https://stackoverflow.com/a/16012416/820068

这应该让你开始。