简体   繁体   English

将数据添加到Datagrid的第二列

[英]Adding data to second column of Datagrid

I have a datagrid DGV . 我有一个datagrid DGV That gridview has a column of "File Name" and is populated by the name of the files you've selected in an openfildialog . gridview有一列"File Name" ,并由您在openfildialog选择的文件名填充。 After performing the calculations I was putting the results in a second datagrid DGV2 which you'll see I've commented out below as I'd like to instead put them on a second column next to their corresponding "File Name" on DGV and just use one gridview . 执行完计算后,我将结果放入第二个datagrid DGV2 ,您将在下面注释掉它,因为我想将它们放在DGV上对应的"File Name"旁边的第二列中,使用一个gridview However this is just taking the last calculation and duplicating it on each row rather than the individual calculations (as they should all be diff) 但是,这只是进行最后的计算并将其复制在每一行上,而不是在各个计算上进行复制(因为它们应全部进行比较)

So it should look like: 所以它应该看起来像:

File1 4.5 
File2 3.5

Instead its just doing 相反,它只是在做

File1  3.5
File2  3.5

I know I'm causing it, I've done something wrong here I'm just not sure how to fix it. 我知道是造成这种情况的原因,我在这里做错了什么,只是不确定如何解决。

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in DGV_Hidden.Rows)
        {
            FileInfo info = new FileInfo();
            {
                var lines = File.ReadAllLines(row.Cells["colfilelocation"].Value.ToString());
                var data = lines.Where(line => (!line.Contains(Data_Start_Point_Identifier) && !line.Contains(FSD__Line_Identifier) && !line.EndsWith("0.00"))).ToList();

                if (data.Count > 1)
                {
                    var line = data[0];
                    var firstsplit = data[1].Split(splitter);
                    info.startvalue = Convert.ToDouble(firstsplit[0]);
                    var secondsplit = data[data.Count - 1].Split(splitter);
                    info.endvalue = Convert.ToDouble(secondsplit[0]);
                }
                info.finalnum = info.startvalue - info.endvalue;
            }
            //DGV2.Rows.Add(info.finalnum);
            for (int i = 0; i < DGV.Rows.Count; i++)
            {
                DGV.Rows[i].Cells["colfiledata"].Value = info.finalnum;
            }
        }
    }

ok, As you said you have similar data /number of rows, You just need to set the value while looping through your hidden grid. 好的,就像您说的那样,您具有相似的数据/行数,您只需要在遍历隐藏的网格时设置值即可。 use the row index of the looping variable to get the correct row. 使用循环变量的行索引获取正确的行。

 private void btnCalculate_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in DGV_Hidden.Rows)
        {
            FileInfo info = new FileInfo();
            {
                var lines = File.ReadAllLines(row.Cells["colfilelocation"].Value.ToString());
                var data = lines.Where(line => (!line.Contains(Data_Start_Point_Identifier) && !line.Contains(FSD__Line_Identifier) && !line.EndsWith("0.00"))).ToList();

                if (data.Count > 1)
                {
                    var line = data[0];
                    var firstsplit = data[1].Split(splitter);
                    info.startvalue = Convert.ToDouble(firstsplit[0]);
                    var secondsplit = data[data.Count - 1].Split(splitter);
                    info.endvalue = Convert.ToDouble(secondsplit[0]);
                }
                info.finalnum = info.startvalue - info.endvalue;
            }
         //set your value here
            DGV.Rows[row.Index].Cells["colfiledata"].Value = info.finalnum;
        }
    }

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

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