【问题标题】:Calculating cells values in a DataGrid计算 DataGrid 中的单元格值
【发布时间】:2013-03-28 05:36:24
【问题描述】:

我有以下 DataGrid(用罗马尼亚语编写):

我想通过这个公式计算“Valoarea”列:

valoarea = Cantitatea * Pret unitar (fara T.V.A.)

我的实际代码给了我错误:

private void produseFacturate_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        switch (e.ColumnIndex)
        {
            case 3:
                var row = produseFacturate.Rows[e.RowIndex];

                double qty, price;

                double.TryParse(row.Cells[2].Value.ToString(), out qty);
                double.TryParse(row.Cells[3].Value.ToString(), out price);

                row.Cells[4].Value = (qty * price).ToString();
                break;
        }
    }

现在,我的问题是:如何使用良好的数据填充行上的特定单元格?

【问题讨论】:

  • 您是否有现有代码,当您在该特定记录上时,它将获取 rows.column 数据,您可以在变量中捕获它以进行计算。问题是你想计算Cell EnterCell Exit,基于Valoarea T.V.A. 值是空还是空..
  • 我不相信数据网格中内置了任何类型的计算引擎(它不是电子表格)。在我的课堂上,我只会将 Valoarea 设为只读属性,然后计算 getter 中的值。
  • @DJKRAZE,你理解我的问题。是我不知道如何获取那个特定的单元格
  • 您目前有任何现有代码吗?如果是这样,请从那里发布您从 DataGridView 中选择的代码,我很确定我们可以为您提供帮助
  • 见我的编辑。它为参数 index 提供 ArgumentOutOfRangeException

标签: c# winforms datagrid


【解决方案1】:

我假设控件实际上是一个 DataGridView?

您可以覆盖 CellFormatting 事件。这是一个简单的例子:

我创建了一个包含 3 列的 DataGridView。

this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Column3 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1,
        this.Column2,
        this.Column3});

this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(405, 150);
this.dataGridView1.TabIndex = 1;
this.dataGridView1.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.dataGridView1_CellFormatting);

我想让第三列成为前两列的乘积。这是我覆盖单元格格式的方法。

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            if (this.dataGridView1.Columns[e.ColumnIndex].Name.Equals("Column3"))
            {
                try {
                    this.dataGridView1.Rows[e.RowIndex].Cells[2].Value =  Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[0].Value) * Convert.ToInt32(this.dataGridView1.Rows[e.RowIndex].Cells[1].Value);
                }
                catch (Exception ex) { }
            }
        }

请注意,每当我更改第一两列中的值时,都会重新计算第三列。

【讨论】:

    【解决方案2】:

    如果有人想知道如何计算 datagridview 的单元格中的值并将结果放在同一行的另一列中,在 datagridview 的 CellEndEdit 事件中,您可以尝试以下方式:

     if (Convert.ToDecimal(datagridPR.CurrentRow.Cells["price_value"].Value) != 0)
            {
                decimal c_Quantity = Convert.ToDecimal(datagridPR.CurrentRow.Cells["quantity"].Value);
                decimal c_Result = c_Quantity * Convert.ToDecimal(datagridPR.CurrentRow.Cells["price_value"].Value);
                datagridPR.CurrentRow.Cells["amount"].Value = c_Result;
            }
    

    每次编辑后按回车键或离开单元格,计算结果立即可以在金额栏看到。您可以扩展代码以处理空值以避免运行时错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2019-05-09
      • 1970-01-01
      相关资源
      最近更新 更多