user2047427
user2047427

Reputation: 5

The name does not exist in the current context c#

I'm trying to bind data with a chart to display it. However when I'm trying to link one of the columns with one of the axis of the chart it comes up with "The name Column1 does not exist in the current context" error. Does anybody know what I'm doing wrong and how to fix it?

Here is my code:

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();
    }

}

}

Upvotes: 0

Views: 3213

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500595

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.

Upvotes: 2

Related Questions