datagridview 计算列成绩之和

今天看到有人问,怎样在DATAGRIDVIEW中计算其中两列的乘积,并赋值给第三列

datagridview 计算列成绩之和_第1张图片

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex==1) { int price=0; bool isprice = Int32.TryParse(this.dataGridView1.CurrentRow.Cells[0].EditedFormattedValue.ToString(), out price); int num = 0; bool isnum =Int32.TryParse(this.dataGridView1.CurrentRow.Cells[1].EditedFormattedValue.ToString(),out num); int sum = price * num; this.dataGridView1.CurrentRow.Cells[2].Value = sum.ToString(); } } private void Form2_Load(object sender, EventArgs e) { DataGridViewTextBoxColumn dgtPrice = new DataGridViewTextBoxColumn(); dgtPrice.HeaderText = "价格"; dgtPrice.Name = "price"; this.dataGridView1.Columns.Add(dgtPrice); DataGridViewTextBoxColumn dgtNum = new DataGridViewTextBoxColumn(); dgtNum.Name = "Num"; dgtNum.HeaderText = "数量"; this.dataGridView1.Columns.Add(dgtNum); DataGridViewTextBoxColumn dgtSum = new DataGridViewTextBoxColumn(); dgtSum.HeaderText = "总额"; dgtSum.Name = "sum"; this.dataGridView1.Columns.Add(dgtSum); }

你可能感兴趣的:(object)