0

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.

User0804
  • 77
  • 2
  • 12
  • `var list = dt.AsEnumerable().Select(r => r.Field("columnName")).ToList();` – Reza Aghaei Dec 12 '18 at 06:00
  • @Reza Aghaei - uc_dt datatable does not contain "Observed(sec)" column. It only contain 7 column before "Observed(sec)" . I created that column using DataGridViewTextBoxColumn. – User0804 Dec 12 '18 at 07:06
  • you just want to get all the data input in `Observed(Sec)` when clicking the button? – Muj Dec 12 '18 at 08:00
  • @Muj - Yes. But by clicking "Save" button which is on main form not Test button on datagridview. – User0804 Dec 12 '18 at 08:03
  • did you try the basic approach? like `commit` the dgv and `loop` through the data? – Muj Dec 12 '18 at 08:18
  • @Muj - Yes I tried the loop,through loop I got value from only first datagridview in usercontrol class. I am unable to take that value in main form. Also I tried DataGridView.CellEndEdit event, through this I am not getting proper values. I don't know about the commit. – User0804 Dec 12 '18 at 08:34

0 Answers0