C# datagridview乘以单元格值

C# datagridview乘以单元格值,c#,winforms,datagridview,C#,Winforms,Datagridview,我对DataGridView有问题。我想将两个单元格相乘,并在第三个单元格中显示结果 我有两个DataGridView。第一个DataGridView从数据库获取数据。在我从第一个DataGridView中选择行并将该行添加到第二个DataGridView之后,第二个DataGridView获取值。列A从数据库的第一个DataGridView获取;在B列中,用户手动插入值 example of second datagridview: Price | Amount | Total 10.

我对DataGridView有问题。我想将两个单元格相乘,并在第三个单元格中显示结果

我有两个DataGridView。第一个DataGridView从数据库获取数据。在我从第一个DataGridView中选择行并将该行添加到第二个DataGridView之后,第二个DataGridView获取值。列A从数据库的第一个DataGridView获取;在B列中,用户手动插入值

example of second datagridview: 
Price |  Amount  |  Total
10.50 | 2        | 21.00
5.20  | 4        | 20.80
7.30  | 5        | 36.50
之后,我想求C列的和,并在文本框中显示和

A列为十进制类型;B列为整数;列C也应该是十进制的

这是一个家伙在互联网上给我的解决方案,但它只适用于手动获取数据的DataGridView,如果数据来自数据库,则不起作用:

decimal sum = 0.0m;

for (int i = 0; i < gridProizvodi.Rows.Count; i++)
{
    DataGridViewRow row = gridProizvodi.Rows[i];
    if (row.IsNewRow) break;
    decimal product = Convert.ToDecimal(row.Cells[1].Value)
                    * Convert.ToInt32(row.Cells[2].Value);

    sum += product;
    row.Cells[3].Value = product;
}

txtCijena.Text= sum.ToString();

有人能帮我找到解决方案吗?

我不确定您是否在使用表单或webapp。。。下面的选项通常适用于webapps,我不知道表单

这通常发生在自动生成GridView列时,即GridView.AutoGenerateColumns=true;时;。它不知道有任何列,因此会给您一个超出范围的异常。您需要将其设置为false,并告诉它要显示哪些列。在第三列的template字段中,您可以简单地将binding属性更改为乘法值

另一种操作是从数据库返回三列,第三列是您的乘数值


希望这有助于…

如果您感到悲伤时线路出现错误。然后,更快的解决方案将是使用列名。 正如@Andres所提到的,使用索引比使用索引更好

从表单设计器中检查列名称,然后按如下方式使用:

decimal product = Convert.ToDecimal(row.Cells[datagridviewTextBoxColumn1.Name].Value)
                * Convert.ToInt32(row.Cells[datagridviewTextBoxColumn2.Name].Value);
或者更好的方法是使用自定义列名称

您的第二个datargidview是预定义列还是以编程方式创建的

如果它们有预定义的名称->则使用它们


如果它们不是->then,因为在第二个datagridview中总是有相同的列,那么最好在designer中创建一次列,例如

decimal sum = 0.0m;
decimal product = 0;

for (int i = 0; i < gridProizvodi.Rows.Count; i++)
{
    DataGridViewRow row = gridProizvodi.Rows[i];
    if (row.IsNewRow) break;
    product = row.item("Price") * row.item("Amount");
    '--- if your price and amount field is numeric type, you dont have to convert it

    sum += product;
    row.item("Total") = product;
}

如果你想在VB.NET中实现它,那么

Dim sum As Decimal = 0D

For i As Integer = 0 To DataGridView1.Rows.Count - 1
    Dim row As DataGridViewRow = DataGridView1.Rows(i)
    If row.IsNewRow Then
        Exit For
    End If

    Dim product As Decimal = Convert.ToDecimal(row.Cells("Column1").Value) * _
            Convert.ToDecimal(row.Cells("Column2").Value)
    'sum += product
    row.Cells("Column3").Value = product
Next

谢谢

我首先在那一行上设置一个断点。row.Cells.length的值是多少?[1]是有效的索引吗?行。单元格[1]。值和行。单元格[2]。值的值是多少?它们可以被解析吗?也许您正在浏览没有单元格的页眉或页脚行?行。单元格[1]i十进制数,行。整数中的单元格[2],行。单元格[3]将是值单元格1*值单元格3十进制数*整数。formtry行的图像。单元格[3]。值=product.ToString。另外,我建议您使用列名代替单元格索引。然后,如果添加列,则不必对代码中的所有索引进行重新排序。try rows.Cells[3]。value=product.ToString。不行,我在用表格。gridview.AutoGenerateColumns=true;我把这个装成假的。我会试试你写的,看看是否有效。tyI在第二个gdv中有预定义的列。我尝试使用列名而不是索引,但id没有work@LukaRoguljic,是否使用了预定义列的名称?以及如何将行添加到第二个dgv?尝试格式化您的代码,但不太成功;--请编辑和改进
Dim sum As Decimal = 0D

For i As Integer = 0 To DataGridView1.Rows.Count - 1
    Dim row As DataGridViewRow = DataGridView1.Rows(i)
    If row.IsNewRow Then
        Exit For
    End If

    Dim product As Decimal = Convert.ToDecimal(row.Cells("Column1").Value) * _
            Convert.ToDecimal(row.Cells("Column2").Value)
    'sum += product
    row.Cells("Column3").Value = product
Next