views:

150

answers:

3

I am new to C#. I have a datagridview with 9 columns. I am simply trying take the value in column 5 and subtract it from column 6. Then display the result in column 9. It seems simple enough.I know it's done in excel all the time. But I just cannot figure this out. Do I need to create a new class with a method called calculate columns? or does the datagridview class have something already built in that can handle this? Again, I am new to C#. Any help would be appreciated.

Thank you,

Oceantrain.

A: 

The sample code below shows how you can retrieve values at a given position (column and row, 0-indexed) and then attempt to convert those values to numbers. You can then store the calculation back into another cell within the DataGridView.

object value1 = dataGridView1[4, 0].Value;
object value2 = dataGridView1[5, 0].Value;

int val1, val2;
if (int.TryParse(value1.ToString(), out val1) && int.TryParse(value2.ToString(), out val2))
{
    dataGridView1[8, 0].Value = val1 - val2;
}
else
{
    MessageBox.Show("cannot subtract, invalid inputs.");
}
Anthony Pegram
Thanks for responding Anthony. I implemented your code in the form class. it was looking good but when I debugged it I entered values in the 2 columns and nothing happened in column 9. Should I implement this code in a specific event handler? I've tried it in the DataGridView_CellEndEdit event handler and again nothing happened. Should I use your code in a buttonclick event?
EB
A: 
public void dataGridView_Cellformatting(object sender, DataGridViewCellFormattingEventArgs args)
{
    if(args.ColumnIndex == 9) //Might be 8, I don't remember if columns are 0-based or 1-based
    {
        DataGridViewRow row = dataGridView.Rows[e.RowIndex];
        args.Value = (int)row.Cells[6].Value - (int)row.Cells[5].Value;
    }
}

Links: CellFormatting event, DataGridViewcellFormattingEventArgs

BlueRaja - Danny Pflughoeft
Thank you blue Raja. I am getting an error saying Error "Operator '-' cannot be applied to operands of type 'System.Windows.Forms.DataGridViewCell' and 'System.Windows.Forms.DataGridViewCell' "any thoughts?
EB
EB - That's because row.Cells[index] will return a DataGridViewCell object. You should call the cell's Value property and parse it into an integer the way Anthony Pegram did.Incidentally Raja's code will only be invoked if you edit something in Column 9, which I'm not sure is what you'd want.
TeeBasins
@EB: Fixed. However, I guess I should have asked; how do you want to use it? I guess I figured you had columns 5 and 6 bound to a datasource, which is usually what you want to do. However, if you are trying to use the datagrid like you would excel, it would be better to handle the CellEndEdit event instead.
BlueRaja - Danny Pflughoeft
OK. I do have the grid bound to a datasource. I'm sorry I did not make that clear. I just want to subtract the value in column 5 from the value in column 6 when the user hits the update button and the result is displayed in column 9 (BALANCE DUE).
EB
A: 

Bind your DataGridView to a DataTable, and give the column a relevant expression. Try extrapolating this example:

        DataTable table = new DataTable();
        table.Columns.Add("Column1", typeof(int));
        table.Columns.Add("Column2", typeof(int));
        table.Columns.Add("Column3", typeof(int), "Column1+Column2");
        dataGridView1.DataSource = table;

where 'dataGridView1' is a typical DataGridView control.

Flynn1179