简体   繁体   中英

how to change cell values in datagridview according to a cell

I have a datagridView in which there are predefined columns. Suppose there are four columns,let's say A,B,C and D. Column A is for quantity and Column B is for rate and Column C is for discount and Column D is for total.I need a textchanged event like thing for when i enter in Column C the value in Column D should automatically change according to calculation as D=A*BC. So please advice me how to achieve this. Thank You.

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) 
{
    dataGridView1.Rows[e.RowIndex].Cells[14].Value =
    ((Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[3].‌​Value)) * (Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[7].V‌​alue)) 
    - Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[13].V‌​alue));/*taxable*/ 
}

If you want to change grid when some cell changed value, you can use CellEndEdit Event

like here:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int editedRow = e.RowIndex;
        int editedColumn = e.ColumnIndex;

        int sum = 0;
        for (int i=0;i< dataGridView1.Columns.Count-1 ;i++)
            sum += dataGridView1[i, e.RowIndex];

        dataGridView1[dataGridView1.Columns.Count-1, editedRow] = sum;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM