0

我正在尝试将数据与图表绑定以显示它。但是,当我尝试将其中一列与图表的轴之一链接时,会出现“当前上下文中不存在名称 Column1”错误。有人知道我做错了什么以及如何解决吗?

这是我的代码:

namespace bike
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }



    private void button1_Click(object sender, EventArgs e)
    {

        var col1 = new List<string>();
        var col2 = new List<string>();
        var col3 = new List<string>();
        var col4 = new List<string>();

        var Column1 = col1.Select<string, int>(q => Convert.ToInt32(q));
        var Column2 = col2.Select<string, int>(q => Convert.ToInt32(q));
        var Column3 = col3.Select<string, int>(q => Convert.ToInt32(q));
        var Column4 = col4.Select<string, int>(q => Convert.ToInt32(q));



        dataGridView1.Columns.Add("col1", "Heart Rate");
        dataGridView1.Columns.Add("col2", "Speed");
        dataGridView1.Columns.Add("col3", "Power");
        dataGridView1.Columns.Add("col4", "Altitude");




        DialogResult result = openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
            {

                int row = 0;
                string line;

                bool isInHRData = false;

                while ((line = sr.ReadLine()) !=null)
                {
                    if (!isInHRData)
                    {
                        if (line != "[HRData]")
                            continue;
                        isInHRData = true;
                        continue;
                    }

                    else if (line.StartsWith("[") && line.EndsWith("["))
                        break;

                    string[] columns = line.Split('\t');
                    if (columns.Length > 0)
                        col1.Add(columns[0]);
                    if (columns.Length > 1)
                        col2.Add(columns[1]);
                    if (columns.Length > 2)
                        col3.Add(columns[2]);
                    if (columns.Length > 3)
                        col4.Add(columns[3]);        

                    /*col1.Add(columns[0]);
                    col2.Add(columns[1]);
                    col3.Add(columns[2]);
                    col4.Add(columns[3]);
                     */

                    dataGridView1.Rows.Add();
                    for (int i = 0; i < columns.Length; i++)
                    {
                        dataGridView1[i, row].Value = columns[i];
                    }
                    row++; 
                }

                int maxSpeed = Column2.Max();
                maxSpeed = maxSpeed / 10;
                string MaxSpeed = Convert.ToString(maxSpeed);
                textBox1.Text = MaxSpeed;

                double aveSpeed = Column2.Average();
                aveSpeed = aveSpeed / 10;
                aveSpeed = Math.Round(aveSpeed, 0);
                string AveSpeed = Convert.ToString(aveSpeed);
                textBox2.Text = AveSpeed;

                double aveHeart = Column1.Average();
                aveHeart = Math.Round(aveHeart, 0);
                string AveHeart = Convert.ToString(aveHeart);
                textBox3.Text = AveHeart;

                int maxHeart = Column1.Max();
                string MaxHeart = Convert.ToString(maxHeart);
                textBox4.Text = MaxHeart;

                int minHeart = Column1.Min();
                string MinHeart = Convert.ToString(minHeart);
                textBox5.Text = MinHeart;

                double avePower = Column3.Average();
                avePower = Math.Round(avePower, 0);
                string AvePower = Convert.ToString(avePower);
                textBox6.Text = AvePower;

                int maxPower = Column3.Max();
                string MaxPower = Convert.ToString(maxPower);
                textBox7.Text = MaxPower;

                double aveAltitude = Column4.Average();
                aveAltitude = Math.Round(aveAltitude, 0);
                string AveAltitude = Convert.ToString(aveAltitude);
                textBox8.Text = AveAltitude;

                int maxAltitude = Column4.Max();
                string MaxAltitude = Convert.ToString(maxAltitude);
                textBox9.Text = MaxAltitude;

            }
        }

    }


    private void button2_Click(object sender, EventArgs e)
    {

        chart1.DataSource = dataGridView1;
        chart1.Series["Series1"].XValueMember = Column1;
        chart1.Series["Series1"].YValueMembers = "test";
        chart1.DataBind();
    }

}

}

4

1 回答 1

2

You're declaring Column1 as a local variable within your (rather long) button1_Click method. If you want it to be part of the state of the object, which will make it available in button2_Click, you should declare it as an instance variable instead.

You might want to consider what will happen if button 2 is clicked before button 1 though.

于 2013-05-12T16:29:39.597 回答