1

I created user control which has datagridview control. And datagridview is getting data from textfile to create row and columns dynamically. I added that user control to panel. One column of dgv is editable. I want that editable column values in a list. I am getting all the values except last row value.

My UI

Code for main Form

    public static List<string> testValue = new List<string>();

    private void SavetoolStripMenuItem1_Click(object sender, EventArgs e)
    {
        List<string> dummy = new List<string>();
        foreach (Control p in panelUC.Controls)
        {
            if (p is uc_protectionTbl1)
            {
                dummy = ((((uc_protectionTbl1)p).obsValue));
                testValue.AddRange(dummy);  //here I want all values
            }
            if (p is uc_performResult)
            { 

            }

        }

Code for userControl

    private List<string> listObs = new List<string>();

    private string obs = null;
    public List<string> obsValue
    {
        get
        {
            foreach (DataGridViewRow row in dgv_uc.Rows)
            {
                obs = row.Cells["Observed(Sec)"].Value.ToString();
                listObs.Add(obs);
            }
            return listObs;
        }
    }


In testValue list index I am getting these values 
 [0] = "1";
 [1] = "2";
 [2] = "3";
 [3] = "";  //here I want "4"

 And listObs list I am getting following values
 [0] = "3";
 [1] = "";  //here I also  want "4"


 My TextFile from where I designed dgv

  TestType=Phase Loset Over Current Protection Test (I>)
  Heading=SrNo||Injected Current||Rated 
  Current||Char||Pickup(xIp)||TMS||STD]||Observed(Sec)
  Value=Test-1||0.4||1||NINV3.0||0.2||0.1||0.95-1.00-1.05
  Value=Test-2||7.0||1||NINV3.0||1.0||0.5||1.68-1.76-1.85

  TestType=Earth Lowset Over Current Protection Test (Ie>)
  Heading=SrNo||Injected Current||Rated 
  Current||Char||Pickup(xIn)||TMS||STD||Observed(Sec)
  Value=Test-3||0.2||1||NINV3.0||0.1||0.1||0.95-1.00-1.05
  Value=Test-4||7.0||1||NINV3.0||1.0||0.5||1.68-1.76-1.85

When I read "SrNo" column instead of "Observed(Sec)" column I got all the value from SrNo column. Then why i am not able to read "Observed(Sec)" column which is editable?

I am not able to understand what I am missing. Please help me to resolve this issue. Thanks in advance!!

User0804
  • 77
  • 2
  • 12

2 Answers2

0

You know that your getter gets called everytime you access the member, which means with your given code - everytime you access the list it will get bigger and bigger - since you aren't clearing or re-creating the list upon access so it grows indefinetly. - I'd suggest fix that issue first than check your values again.

Change your member obsValue to this:

//remove the fields obsValue and obs, and use local variables instead
//private List<string> listObs = new List<string>();
//private string obs = null;

public List<string> obsValue
{
    get
    {
        //create a new list everytime the getter of this member gets called
        var listObs = new List<string>();
        foreach (DataGridViewRow row in dgv_uc.Rows)
        {
            //declare obs here since we only use it here
            var obs = row.Cells["Observed(Sec)"].Value.ToString();
            listObs.Add(obs);
        }
        return listObs;
    }
}
Rand Random
  • 7,300
  • 10
  • 40
  • 88
0

Try the for loop instead

        for (int i = 0; i < dgv_us.Rows.Count(); i++)
        {
           string obs = dgv_uc.Rows[i].Cells["Observed(Sec)"].Value.ToString();
           listObs.Add(obs);
        }
TempoClick
  • 169
  • 12