dataGridView的计算问题(相乘)

来源:互联网 发布:html js图片轮播 编辑:程序博客网 时间:2024/05/16 18:38

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;
}