DataGridView自动计算单元格 [英] DataGridView Auto-calculate cells

查看:231
本文介绍了DataGridView自动计算单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想在一行中的datagridview控件中将两个或多个单元格的值相乘或相加,并且在单元格中输入数据后也应该自动保存在数据库中



我不知道使用什么代码,所以我没有在这个讨论中加入任何代码。

我的图片链接

< a href =http://tinypic.com/view.php?pic=142bryf&s=8> http://tinypic.com/view.php?pic=142bryf&s=8

在此先感谢

Hi guys, I want to multiply or add the values of two or more cells in a datagridview control in a row and and should also be auto-saved in the database after entering datas in the cell

I don't know what code to use, so I did not put any codes in this discussion.
My Image Link
http://tinypic.com/view.php?pic=142bryf&s=8
Thanks In Advance

推荐答案

您好... ...



使用以下指南用于在DGV的单个单元格上执行多个或添加操作,以便为DGV中的任何其他单元格生成自动计算值





如果您真的想要通过Heart进行编程...您可以将以下步骤转换为您的Code Sytle ...



将以下代码放入DGV的 CellValueChanged 活动。

Hi Fellow...

Used following guideline for performing Multiple or Add operations on DGV's individual cells to generate Auto-Calculated values for any other Cell in DGV


If you Really want to do programming by Heart...You can transform following steps into your Code Sytle...

Put following code into DGV's CellValueChanged Event.
if (e.RowIndex > -1)
{ 
     if (DGV.Rows[e.RowIndex].Cells[1].FormattedValue.ToString() != "")
        {
          //look for which column is being Changed and 
         //After checking which Cell's value is Changed, 
         //Calculate the "Total Amount" value for Auto-Calculated Cell as follows...
          if (e.ColumnIndex == 1 || e.ColumnIndex == 2)
          DGV.Rows[e.RowIndex].Cells[3].Value = 
                (decimal)(Cells[1].value)  * (decimal)(Cells[2].value);
         }     
}





//现在计算每列值的总和......

//您可以使用以下代码段



//Now to Calculate Sum of Each Column's Values...
//you can use following code snippet

<pre lang="cs">decimal total = 0;
    for (int i = 0; i &lt; rows; i++)
       total += decimal)(DGV.Rows[i].Cells[3].Value);
           </pre>





注意: - 这只是指南,我知道它正在工作中我在我的项目中使用它。在这里,我只是给出一个你要寻找的要点...



和将这些数据保存到数据库中的代码你应该自己尝试一下..我认为你非常熟悉从数据库保存/检索数据。





快乐编程......: - )



Note :- This is just Guideline and I know it's working I used it in my one of project. Here I just give a major points that you are looking for...

And The Code for Saving these DATA into database you should have to Try it by yourself.. I think you are very familiar with saving / retrieving data from Database.


Happy Programming...:-)


没有什么比自动保存了。你必须处理一些 DataGridView 的事件或任何其他控件的事件,如Button Click或其他东西。



你可以尝试处理 DataGridView.CellLeave 事件 [ ^ ]。在里面检索单元格值并执行所需的操作。之后连接到数据库并插入。
There is nothing like Auto-Save. You have to handle some event of DataGridView or any other control's event like a Button Click or something.

You can try to handle the DataGridView.CellLeave Event[^]. Inside that retrieve the cell value and do the needed operations. After that connect to database and insert that.


这篇关于DataGridView自动计算单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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