我有一个从CSV文件中填充的datagridview。我现在想表示表中的数据。我尝试了一些代码,但它不起作用。按下按钮后,我希望它绘制图表。
以下是我的全部代码:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class HRData
{
public int? HeartRate { get; set; }
public int? Speed { get; set; }
public int? Power { get; set; }
public int? Altitude { get; set; }
public override string ToString()
{
return String.Format("Heart rate={0}, Speed={1}, Power={2}, Altitude={3}", HeartRate, Speed, Power, Altitude);
}
}
public static class HRDataReader
{
static private int? ConvertValue(string[] values, int index)
{
if (index >= values.Length)
return null;
int value;
if (int.TryParse(values[index], out value))
return value;
return null;
}
static public IList<HRData> Read(string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
using (StreamReader sr = new StreamReader(fileName))
{
string line;
// First: Skip to the correct section.
while ((line = sr.ReadLine()) != null)
if (line == "[HRData]")
break;
// Now: Read the HRData
List<HRData> data = new List<HRData>();
while ((line = sr.ReadLine()) != null)
{
if (line.StartsWith("[") && line.EndsWith("]"))
break;
line = line.Trim().Replace("\t", " "); // Remove all tabs.
while (line.Contains(" ")) // Remove all duplicate spaces.
line = line.Replace(" ", " ");
string[] values = line.Split(' '); // Split the line up.
data.Add(new HRData
{
HeartRate = ConvertValue(values, 0),
Speed = ConvertValue(values, 1),
Power = ConvertValue(values, 2),
Altitude = ConvertValue(values, 3)
});
}
return data;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
IList<HRData> data = HRDataReader.Read(openFileDialog1.FileName);
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "HeartRate", HeaderText = "Heart rate", DataPropertyName = "HeartRate" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Speed", HeaderText = "Speed", DataPropertyName = "Speed" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Power", HeaderText = "Power", DataPropertyName = "Power" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name = "Altitude", HeaderText = "Altitude", DataPropertyName = "Altitude" });
dataGridView1.DataSource = data;
//int
int maxSpeed = data.Max(x => x.Speed.Value);
maxSpeed = maxSpeed / 10;
string MaxSpeed = Convert.ToString(maxSpeed);
textBox1.Text = MaxSpeed;
double aveSpeed = data.Average(x => x.Speed.Value);
aveSpeed = aveSpeed / 10;
aveSpeed = Math.Round(aveSpeed, 0);
string AveSpeed = Convert.ToString(aveSpeed);
textBox2.Text = AveSpeed;
double aveHeart = data.Average(x => x.HeartRate.Value);
aveHeart = Math.Round(aveHeart, 0);
string AveHeart = Convert.ToString(aveHeart);
textBox3.Text = AveHeart;
int maxHeart = data.Max(x => x.HeartRate.Value);
string MaxHeart = Convert.ToString(maxHeart);
textBox4.Text = MaxHeart;
int minHeart = data.Min(x => x.HeartRate.Value);
string MinHeart = Convert.ToString(minHeart);
textBox5.Text = MinHeart;
double avePower = data.Average(x => x.Power.Value);
avePower = Math.Round(avePower, 0);
string AvePower = Convert.ToString(avePower);
textBox6.Text = AvePower;
int maxPower = data.Max(x => x.Power.Value);
string MaxPower = Convert.ToString(maxPower);
textBox7.Text = MaxPower;
double aveAltitude = data.Average(x => x.Altitude.Value);
aveAltitude = Math.Round(aveAltitude, 0);
string AveAltitude = Convert.ToString(aveAltitude);
textBox8.Text = AveAltitude;
int maxAltitude = data.Max(x => x.Altitude.Value);
string MaxAltitude = Convert.ToString(maxAltitude);
textBox9.Text = MaxAltitude;
}
}
private void button2_Click(object sender, EventArgs e)
{
chart1.DataSource = hRDataBindingSource;
chart1.Series["Series1"].XValueMember = Speed;
chart1.Series["Series1"].YValueMembers = "test";
chart1.DataBind();
}
}
}