How to get values of same column from n number of user control datagridview in main form button click event

79 Views Asked by At

I am working on windows form application. I created user control which has datagridview control. I added dynamically 10 user control in a panel(which is in main form) which has "Observed(sec)" column in common. In this column user will enter values.And click on save button. All the values will get saved in text file.

My question is : I added everytime a new user control in a panel. Then how to get values from Observed column to save button?

enter image description here

Main form code

      private void addUserContol()
      { 
        string[] lines = File.ReadAllLines(filename);
        string[] newline;
        string[] splitLine;

        try
        {
            int i = 0;
            string lineText;
            for (i = 0; i < lines.Length; )
            {
                uc_protectionTbl1 objUC = new uc_protectionTbl1(); //user control object is created here
                uc_groupLbl objGrouplbl = new uc_groupLbl();
                uc_testResult result = new uc_testResult();
                uc_performResult objperform = new uc_performResult();
                var wordCount = new Dictionary<string, int>();

                    lineText = lines[i++];
                    if (lineText != "")
                    {
                        if (lineText.Contains("Title"))
                        {
                            splitLine = lineText.Split('=');
                            lbl_title.Text = splitLine[1];
                        }
                        if (lineText.Contains("GroupLabel"))
                        {
                            splitLine = lineText.Split('=');
                            objGrouplbl.grouplblvalue = splitLine[1];
                            panelUC.Controls.Add(objGrouplbl);
                        }
                        if (lineText.Contains("Performance Test"))
                        {
                            panelUC.Controls.Add(objperform);
                        }
                        if (lineText.Contains("Result"))
                        {
                            splitLine = lineText.Split('=');
                            result.addTest = splitLine[1];
                            panelUC.Controls.Add(result);
                        }
                        if (lineText.Contains("TestType"))
                        {
                            splitLine = lineText.Split('=');
                            objUC.testType = splitLine[1];

                            lineText = lines[i++];

                            if (lineText.Contains("Heading"))
                            {
                                string[] delimiter = { "=", "||" };
                                splitLine = lineText.Split(delimiter, StringSplitOptions.None);
                                newline = splitLine.Skip(1).ToArray();
                                objUC.numColumn = newline.Count();
                                objUC.columnheading = newline;
                            }

                            while (i < lines.Length)
                            {
                                lineText = lines[i++];
                                if (lineText != "")
                                {
                                    if (lineText.Contains("Value"))
                                    {
                                        string[] delimiter = { "=", "||" };
                                        wordCount.Add(lineText, 1);
                                        objUC.numRow = wordCount.Values.Count();
                                        string[][] arrayofarray = wordCount.Select(x => (x.Key.Split(delimiter, StringSplitOptions.None)).Skip(1).ToArray()).ToArray();
                                        objUC.rowValues = arrayofarray;
                                    }
                                    else
                                    {
                                        panelUC.Controls.Add(objUC); //here I added user control in panel
                                        i = i - 1;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    } 
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
        }
    }
    private void SavetoolStripMenuItem1_Click(object sender, EventArgs e)
    {
       //Here I want all the values from observed(Sec) column
    }
 }

Code for user Control

  public partial class uc_protectionTbl1 : UserControl
{
    public string testType { get; set; }
    public int numRow { get; set; }
    public int numColumn { get; set; }
    public string[] columnheading { get; set; }
    public string[][] rowValues { get; set; }

    public uc_protectionTbl1()
    {
        InitializeComponent();       
    }

    private void uc_ProtectionTbl1_Load(object sender, EventArgs e)
    {
        designDT();  
    }

   public DataTable uc_dt = new DataTable();
   private void designDT()
    {
        try
        {
            foreach (var column in columnheading)
            {
                uc_dt.Columns.Add(new DataColumn(column.ToString()));
            }
            foreach (var row in rowValues)
            {
                uc_dt.Rows.Add(row);
            }  
            dgv_uc.DataSource = uc_dt;

            dgv_uc.Columns.Cast<DataGridViewColumn>().ToList().
              ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);

            lbl_testType.Text = testType;

            DataGridViewTextBoxColumn obs = new DataGridViewTextBoxColumn();   //Here I added textbox column 
            obs.HeaderText = "Observed(Sec)";
            obs.Name = "Obs";
            obs.ReadOnly = false;
            this.dgv_uc.Columns.Add(obs);

            var button = new DataGridViewButtonColumn();
            button.Name = "TestButton";
            button.HeaderText = "Test_Button";
            button.Text = "Test";
            button.UseColumnTextForButtonValue = true;

            this.dgv_uc.Columns.Add(button);

            sizeDGV(dgv_uc);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
  }

Please help me to resolve this issue. I am new in developement. From last 15 days searched google but not getting proper solution. Thanks in advance.

0

There are 0 best solutions below