如何将数据Gridview乘以两列并在另一列中显示结果 [英] How to Multiply Data Gridview two columns and show the result in another column

查看:202
本文介绍了如何将数据Gridview乘以两列并在另一列中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的gridview(订单):

I have a gridview (Order) with three columns:


  1. 价格

  2. 数量

  3. 总计

我想乘以价格具有 Quantity ,并在dataGridview的 Total 列中显示结果。

I want to multiply Price with Quantity and show the result in Total column of dataGridview.

记住:我的dataGridview没有与任何表绑定。

Remember: my dataGridview isn't bind with any table.

我正在尝试使用此代码来实现我的目标,但这不起作用,意味着没有价值。不会被返回:

I am trying this code to achieve my goal but this isn't working means value isn't being returned:

private void totalcal()
{
    // is the foreach condition true? Remember my gridview isn't bound to any tbl
    foreach (DataGridViewRow row in gvSale.Rows)
    {
        int a = Convert.ToInt32(row.Cells[3].Value) *     Convert.ToInt32(row.Cells[4].Value);  // value is null why??
        row.Cells[5].Value = a;
    }
}

这是我在按钮上调用的方法点击。 (这不是在上面的代码中定义的工作原因)

This is the method which I am calling on a button click. (It is not working reason define inside of my code above)

而且我想知道哪个是适合此计算的Datagridview事件?我不想计算按钮点击总数

And plus I want to know which is the suitable Datagridview event for this calculation?? I don't want to calculate the total on button click

推荐答案

尝试

int.Parse(row.Cells[3].Value.toString()) * int.Parse(row.Cells[4].Value.toString())

Convert.ToInt32(row.Cells[3].Value) * Convert.ToInt32(row.Cells[4].Value)

如果您不希望单击按钮,可以随时调用此方法。在gvSale的行填充操作完成后调用它。

And you know you can call this method anytime, if you dont want it to be with button click. Call it after gvSale's row populating operations finished.

编辑

我想你希望在用户输入价格数量时完成计算。为此,您需要为您的datagridview编写一个 EditingControlShowing 方法。这是一段代码。

I guess you want the calculations to be done while the user is entering Price or Quanitity. For that you need to write a EditingControlShowing method for your datagridview. Here's a piece of code. I tested it actually and got it working.

InitializeComponent(); 之后,将此代码添加到您的主类定义中行

Add this code in your main class definition after InitializeComponent(); line

gvSale.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.gvSale_EditingControlShowing);

然后添加此方法:

TextBox tb = new TextBox(); // this is just a textbox to use in editing control
int Price_Index = 3; // set this to your Price Column Index
int Quantity_Index = 4; // set this to your Quantity Column Index
int Total_Index = 5; // set this to your Total Column Index

private void gvSale_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
  if (gvSale.CurrentCell.ColumnIndex == Price_Index || gvSale.CurrentCell.ColumnIndex == Quantity_Index)
  {
    tb = e.Control as TextBox;
    tb.KeyUp += new KeyEventHandler(Calculate_Total);
  }
}

private void Calculate_Total(object sender, KeyEventArgs e)
{
  int Price_Value = 0;
  int Quantity_Value = 0;
  int.TryParse(gvSale.CurrentCell.ColumnIndex != Price_Index ? gvSale.CurrentRow.Cells[Price_Index].Value.ToString() : tb.Text, out Price_Value);
  int.TryParse(gvSale.CurrentCell.ColumnIndex != Quantity_Index ? gvSale.CurrentRow.Cells[Quantity_Index].Value.ToString() : tb.Text, out Quantity_Value);
  gvSale.CurrentRow.Cells[Total_Index].Value = Price_Value * Quantity_Value;
}

这篇关于如何将数据Gridview乘以两列并在另一列中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆