简体   繁体   English

C# 数据网格视图到数据网格视图

[英]C# datagridview to datagridview

I have 2 datagridviews the first one datagridview1 - input 4 lists of numbers then i get the Min from each Column(4) i want to subtract the MinVal from datagridview1 and display in datagridview2.我有 2 个 datagridviews,第一个 datagridview1 - 输入 4 个数字列表,然后我从每个 Column(4) 中得到 Min 我想从 datagridview1 中减去 MinVal 并显示在 datagridview2 中。 Im using an array icant seem to get the data type right.我使用的数组似乎无法正确获取数据类型。 Datagridview page this image shows the loops are working i want the lowest in each column to by subtracted by each value in each column. Datagridview 页面此图像显示循环正在工作我希望每列中的最低值减去每列中的每个值。 the problem is getting the array to work properly.问题是让阵列正常工作。

my problem is the error im getting "System.InvalidCastException: 'Specified cast is not valid.'我的问题是我收到“System.InvalidCastException:‘Specified cast is not valid.’”的错误。 "

Error Message错误信息

Im stuck with this and i know its simple.我坚持这个,我知道它很简单。 can someone help with this?有人可以帮忙吗?

       // Read data in

        for (int i = 0; i < Spindles; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                

              machineRadialDataArray[i, j] = (decimal)dataGridView1.Rows[i].Cells[j].Value;

            }
        }


        
        // Display Data after Calc
        for (int i = 0; i < Spindles; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                
                dataGridView2.Rows[i].Cells[j].Value = machineRadialDataArray[i, j] - MinValTest[j];
            }
        }
        //

Instead of casting it as a decimal, have you tried converting it?您是否尝试过将其转换为小数,而不是将其转换为小数? I found an old example where I did something similar.我找到了一个旧的例子,我做了类似的事情。

Using system;使用系统;

machineRadialDataArray[i, j] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[j].Value); machineRadialDataArray[i, j] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[j].Value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM