0

I have a 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. 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. 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;
            }
        }
    }
5
  • The last thing your code seems to be doing is setting all rows that column to the info.finalnum of that last file. I think you only want to set for that particular file, not all. Commented Sep 13, 2017 at 14:35
  • you are setting every row in DGV to info.filenum in the for loop, which i bet is the value you are seeing duplicated. Commented Sep 13, 2017 at 14:37
  • Your both correct. I see that also. And I hate to sound moronic here but I'm not sure how to individualize this. Is it easy enough to just edit what I have? Any code suggestions would be greatly appreciated. Commented Sep 13, 2017 at 14:44
  • DGV_Hidden and DGV contains same number of rows? or include what is the data in DGV and DGV_HIdden Commented Sep 13, 2017 at 14:49
  • @Hybridzz Yes same number of rows. DGV just has the file names where as DGV_Hidden is the actual file location that is being used that doesn't need seen. Commented Sep 13, 2017 at 14:56

1 Answer 1

1

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;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yep that's all I needed. Seems silly now. Good lookin out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.