How can I show the results of an operation in a datagridview generating a new column?

0

I want to generate a new column (called "Column1") showing in it the result of each of the values of an existing column. With this code I get an error saying that Column 1 does not exist and I can not explain why it is supposed to be generated before carrying out operations. I hope you can help me please.

     private void btnCalcular_Click_1(object sender, EventArgs e)
    {
        DataGridViewTextBoxColumn columna1 = new   DataGridViewTextBoxColumn();
        columna1.HeaderText = "Columna1";
        columna1.Width = 200;
        dataGridView1.Columns.Add(columna1);

        for (int i = 0; i < (this.dataGridView1.RowCount - 1); i++)
        {
            this.dataGridView1.Rows[i].Cells["Columna1"].Value.ToString();

            decimal eff = decimal.Parse(txtEFFsim.Text);
            int fijo2 = int.Parse(lblfijo2.Text); // /100
            decimal resp = eff / fijo2;
            lblresul.Text = resp.ToString();

            int operador = int.Parse(txtQtyOp.Text);
            decimal ratee = int.Parse(txtRate.Text);
            decimal efff = resp; 
            decimal res4 = operador / ratee * efff;
            txt4.Text = res4.ToString(); 

            this.dataGridView1.Rows[i].Cells["Columna1"].Value.ToString();
        }
    }

    //List<Person> p1 = new List<Person>();
    //p1.Add(new Person() { Id = 1, EFF_CWS = txt4.Text, });
    //dataGridView1.DataSource = p1;


    private void frmDataGridCalculation_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'sisIndustrialDataSet2.CWS' table. You can move, or remove it, as needed.
        this.cWSTableAdapter.Fill(this.sisIndustrialDataSet2.CWS);
    }
}}   

UPDATE: I already generate the column, I just need to fill it with the results obtained by calculating the values I have in my existing column. I get an error in the following line:

  this.dataGridView1.Rows[i].Cells["Columna1"].Value.ToString();

The error says: An unhandled exception of type 'System.NullReferenceException' Object reference not set to an instance of an object.

    
asked by Eduardo Parra 14.12.2016 в 00:06
source

1 answer

1

The problem with the code that you put is that you are not giving name to the column, just put a text to the header. Try adding columna1.Name = "Columna1"; before dataGridView1.Columns.Add(columna1); . With that I should not throw that mistake at you.

    
answered by 14.12.2016 в 08:59