Datagrid - unbound

VS 2005 std.

Using an unbound datagridview that has 5 columns, when columns 1,2 or 3 change I want to show the total for columns 1+2+3 in column 4.

Column 5 is a tickbox, how can you tell if its ticked or not.

Thanks in advance.



Answer this question

Datagrid - unbound

  • Lee John

    The problem seems to be null values.

    How would I check for that


  • Alger

    you can handle this in CellEndEdit event



    if (e.ColumnIndex==1||e.ColumnIndex == 2 || e.ColumnIndex == 3 )
    {
    int colOne= (int)dgvInvoiceItems.Rows[e.RowIndex].Cells[1].Value;
    decimal ColeTwo= (decimal)dgvInvoiceItems.Rows[e.RowIndex].Cells[2].Value;
    decimal ColThree= (decimal)dgvInvoiceItems.Rows[e.RowIndex].Cells[3].Value;
    decimal total = colOne +colTwo + colThree;
    dgvInvoiceItems.Rows[e.RowIndex].Cells[4].Value = total;
    }

     



  • captainsina

    This worked fine

    try

    {

    double Col1 = double.Parse (dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString ());

    double Col3 = double.Parse (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString ());

    double Col4 = double.Parse (dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString ());

    double Col5 = double.Parse (dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString ());

    double total = ((Col3 * Col4) * Col5) * Col1;

    dataGridView1.Rows[e.RowIndex].Cells[7].Value = string.Format ("{0,10:N4}",total);

    }

    catch{}

    Thanks everso.


  • Stretchcoder

    I tried that and I get this error:

    'InvalidCast Exception'


  • Eswans2000

    the previous code assumed that the columns ValueType is int,decimal,decimal and decimal

    you can change your column ValueType to make the previous code work Or

    Change it as the following



    if (e.ColumnIndex == 1 || e.ColumnIndex == 2 || e.ColumnIndex == 3)
    {
    int colOne = int.Parse(dgvInvoiceItems.Rows[e.RowIndex].Cells[1].Value.ToString());
    decimal ColeTwo = decimal.Parse(dgvInvoiceItems.Rows[e.RowIndex].Cells[2].Value.ToString());
    decimal ColThree = decimal.Parse(dgvInvoiceItems.Rows[e.RowIndex].Cells[3].Value.ToString());
    decimal total = colOne + colTwo + colThree;
    dgvInvoiceItems.Rows[e.RowIndex].Cells[4].Value = total;
    }

     

    tell me the result



  • Datagrid - unbound