【问题标题】:How to perform multiplication on two column for datagrid view and assigning it in third column?如何对数据网格视图的两列执行乘法并将其分配到第三列?
【发布时间】:2013-11-28 06:18:04
【问题描述】:

这是我的代码,我正在尝试显示 Rate * Supply 列值的乘法并将其分配给数据网格视图中的 Amount 列:

try
{
    Query = "Select  id,Code,Description,Rate,Cust_Id,Supply,Empty,Amount,Received from Items ";
    adap = new SQLiteDataAdapter(Query, GlobalVars.conn);
    ds = new DataSet();
    adap.Fill(ds, "Items");
    dtgVOuchers.DataSource = ds.Tables[0];

    ds.Tables[0].Columns["Amount"].DefaultValue = (ds.Tables[0].Columns["Rate"].DefaultValue) * (ds.Tables[0].Columns["Supply"].DefaultValue); //error

    //dtgVOuchers.Rows.Clear();
    ds.Tables[0].Clear();
    dtgVOuchers.Refresh();
}

有谁知道我如何在数据网格视图中完成此功能? . .请帮助我更正此代码。谢谢

【问题讨论】:

  • 注意:我想在代码中相乘而不是在查询中。谢谢

标签: c# winforms datagridview datagridviewcolumn


【解决方案1】:

只需使用查询

Select  id,Code,Description,Rate,Cust_Id,Supply,Empty,isnull(Rate,0)*isnull(Supply,0) as [Amount],Received from Items 

【讨论】:

  • SQL 甚至没有标记在这个上,所以你可能需要重新考虑你的答案
  • 是的,我知道,但用户没有提到他想在代码中相乘,而是可以在查询中轻松完成。
  • 错误: SQLiteManager: 可能的 SQL 语法错误: Select id,Code,Description,Rate,Cust_Id,Supply,Empty,isnull(Rate,0)*isnull(Supply,0) as [Amount],接收自项目 [“isnull”附近:语法错误] 异常名称:NS_ERROR_FAILURE 异常消息:组件返回失败代码:0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
  • @Terror.Blade - 哈哈......当你假设一个没有标准的语言的答案时会发生这种情况。祝你好运!
  • 删除 isnull 只使用 rate*supply
【解决方案2】:
DataTable myDataTable = new DataTable();

      DataColumn quantityColumn = new DataColumn("Quantity");
      DataColumn rateColumn = new DataColumn("Rate");
      DataColumn priceColumn = new DataColumn ("Price");

      quantityColumn.DataType = typeof(int);
      rateColumn.DataType = typeof(int);

      myDataTable.Columns.Add(quantityColumn);
      myDataTable.Columns.Add(rateColumn);
      myDataTable.Columns.Add(priceColumn);

      //Set the Expression here
      priceColumn.Expression = "Quantity * Rate";

【讨论】:

  • 因为我没有在数据表中添加列。所以请告诉我如何为这种情况编写代码。我正在尝试像您的代码一样,但它给出了错误:ds.Tables[0] .Columns["Amount"].Expression = (ds.Tables[0].Columns["Rate"].Expression) * (ds.Tables[0].Columns["Supply"].Expression);
【解决方案3】:
            foreach (DataGridViewRow row in dtgVOuchers.Rows)
            {
                row.Cells[dtgVOuchers.Columns["Amount"].Index].Value = (Convert.ToDouble(row.Cells[dtgVOuchers.Columns["Rate"].Index].Value) * Convert.ToDouble(row.Cells[dtgVOuchers.Columns["Supply"].Index].Value));

            } 

【讨论】:

    【解决方案4】:

    使用下面的代码..运行一个循环来更新所有行中的 Amount 列

    注意:虽然这种方法不是首选,因为它会影响性能。所以在 sql 查询中进行此计算。我不明白在 SQL 查询中不进行多重运算的原因..

      foreach (DataRow row in ds.Tables[0].Rows)
           {
              row["Amount"]= Convert.ToDecimal(row["Rate"])* Convert.ToDecimal(row["Supply"]);
              ds.Tables[0].AcceptChanges();        
           } 
        dtgVOuchers.DataSource = ds.Tables[0];
        dtgVOuchers.Refresh();
    

    【讨论】:

    • Error : It is given invalid cast exception ....Object can't be cast from DBNull to other types............请帮助消除此错误
    • 为您编辑了答案。如果有任何问题,请告诉我。
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多