使用dataGridView列进行数学运算,然后在textBox中显示最终值 [英] Math operation with dataGridView columns row by row then display final value in textBox

查看:207
本文介绍了使用dataGridView列进行数学运算,然后在textBox中显示最终值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在想,做数据格式数据的最好办法是什么。我有这个datagridview,我需要逐行乘以列castka与počet,然后+下一行,然后+下一行,直到Im结束,然后显示最终的值在textBox中,因为我是初学者c#和sql我想和你讨论一下(代码是欢迎:))

I've been thinking about what is the best way to do my datagridview math. I have this datagridview which I need to go row by row multiplying column "castka" with počet" and then + the next row then + next row until Im on the end and then display the final value in textBox. As I'm beginner in c# and sql I wanted to discuss this with you for first. (codes are welcome :) )

非常感谢你。

这是我的表格如何。

有人会推荐我吗?

 void calculateProductTwoColumns(int castkaIndex, int pocetIndex)
    {
        double[] outVals = new double[tot_rows + 1]; //Array including product of both columns per row
        int curRow = 0;
        foreach (DataGridViewRow row in dtg_ksluzby.Rows)
        {
            curRow = curRow + 1;
            outVals[curRow] = (double)row.Cells[4].Value * (double)row.Cells[6].Value;
          //  MessageBox.Show(""+ outVals.ToString());
            kpriplac.Text = outVals.ToString();
        }

    }

这是我如何调用它form_load

this is how I call it on form_load

 calculateProductTwoColumns( 4, 6);


推荐答案

DataGridView 是用于数据显示的控件;如果要执行任何类型的数学运算,您将必须创建一个算法来检索数据并进行计算。如果您从数据库获取数据,则通过依靠查询执行操作将更快。通过单元格迭代不是太困难,既不写入 TextBox ;这里你有一个执行两个动作的示例函数(在$ code> dataGridView1 和 textBox1 之间):

DataGridView is a control for data-displaying purposes; if you want to perform any kind of mathematical operation you would have to create an algorithm to retrieve the data and carry out the calculations. If you are getting the data from a DB, it would be quicker to perform the operations by relying on queries. Iterating through the cells is not too difficult, neither writing to a TextBox; here you have a sample function performing both actions (between dataGridView1 and textBox1):

private void calculateSumColumn(int curColumn)
{
    double valueCurColumn = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
         valueCurColumn = valueCurColumn + (double)row.Cells[curColumn].Value;
    }

    textBox1.Text = valueCurColumn.ToString();
}

适用于您的具体情况:

private void calculateProductTwoColumns(int castkaIndex, int pocetIndex, int tot_rows)
{
    double[] outVals = new double[tot_rows + 1]; //Array including product of both columns per row
    int curRow = 0;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
         curRow = curRow + 1;
         outVals[curRow] = (double)row.Cells[castkaIndex].Value * (double)row.Cells[pocetIndex].Value;
    }

}

这篇关于使用dataGridView列进行数学运算,然后在textBox中显示最终值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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