dataGridView的计算问题(相乘)
方法一:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// 总数量=每串数量*串数
if (e.ColumnIndex == 4 || e.ColumnIndex == 5)
{
try
{
dataGridView1.Rows[e.RowIndex].Cells[6].Value = (Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value) * Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)).ToString();
}
catch (Exception)
{
MessageBox.Show("请输入数字");
}
}
}
方法二:
private void button1_Click(object sender, EventArgs e)
{
DataTable dt2 = new DataTable();
dataGridView1.Focus();
dt2.Columns.Add("每串数量",typeof(Int32));
dt2.Columns.Add("串数",typeof(Int32));
dt2.Columns.Add("数量",typeof(Int32), "每串数量*串数");
dataGridView1.DataSource = dt2;
}