mercredi 14 février 2018

For Loop Control Location

I am making a for loop for dynamically adding checkboxes based on Column Names from a DataGridView. I am importing Excel Files to DataGridView. But when I change from another tab page [a tab page is an equivalent to an excel sheet], it keeps on dynamically adding values from int dynamicHeight continuously.

TAB PAGE 1:

Tab Page 1

TAB PAGE 2

enter image description here

Here is the code:

public static void ReadExcel(ComboBox cboSheet, TabControl tabCon, ComboBox cboColumn)
    {
       try
       {
           OpenFileDialog openFileDialog = new OpenFileDialog();
           openFileDialog.Filter = "Excel Files| *.xls; *xlsx";
           openFileDialog.ShowDialog();

           if (!string.IsNullOrEmpty(openFileDialog.FileName))
           {
               OleDbcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " + openFileDialog.FileName + "; Extended Properties='Excel 12.0; HDR=Yes; IMEX=1;'");
               OleDbcon.Open();

               DataTable dt = OleDbcon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
               OleDbcon.Close();

               cboSheet.Items.Clear();

               int width = 1330;
               int height = 575;
               int dynamicHeight = 20;
               int padding = 0;   
               //1338, 590
               //decimal num;
               //int kpi = 2;

               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   if (!dt.Rows[i]["Table_Name"].ToString().Contains("FilterDatabase") )
                    {
                        String sheetName = dt.Rows[i]["Table_Name"].ToString();
                        sheetName = sheetName.Replace(@"'", "");
                        sheetName = sheetName.Replace("$", "");

                        TabPage tp = new TabPage(sheetName);
                        DataGridView dataGridView = new DataGridView();
                        Panel panelCol = new Panel();



                        ComboBox cboKPI = new ComboBox();
                        ComboBox cboCol = new ComboBox();
                        Button btnValidate = new Button();
                        Button btnValidateAll = new Button();
                        Button btnValidateKPI = new Button();
                        Label lblKPI = new Label();
                        Label lblCol = new Label();




                        lblKPI.Text = "KPI Column:";                 // .Text Label Names
                        lblCol.Text = "Decimal Column:";


                        btnValidate.Text = "Validate";
                        btnValidateAll.Text = "Validate All";
                        btnValidateKPI.Text = "Validate KPI";

                        tabCon.Controls.Add(tp);                //Add Page and Controls
                        tp.Controls.Add(cboKPI);

                        tp.Controls.Add(cboCol);
                        tp.Controls.Add(lblKPI);
                        tp.Controls.Add(lblCol);
                        tp.Controls.Add(lblCol);
                        tp.Controls.Add(panelCol);

                        tp.Controls.Add(btnValidate);
                        tp.Controls.Add(btnValidateAll);
                        tp.Controls.Add(btnValidateKPI);



                        tp.Controls.Add(dataGridView);
                        CreateDataGrid(dataGridView, sheetName, cboKPI, panelCol);

                       CheckBox[] chk = new CheckBox[dataGridView.Columns.Count - 1];
                        for (int z = 0; z < dataGridView.Columns.Count - 1; z++ )
                        {
                            chk[z] = new CheckBox();
                            chk[z].Name = dataGridView.Columns[z].Name ;
                            chk[z].Text = dataGridView.Columns[z].Name;
                            chk[z].AutoCheck = true;
                            chk[z].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
                            panelCol.Controls.Add(chk[z]);

                            panelCol.Size = new Size(120, dynamicHeight);
                            panelCol.Controls.Add(chk[z]);
                            chk[z].Location = new Point(0, dynamicHeight);
                            chk[z].Size = new Size(120, 21);
                            panelCol.BackColor = Color.White;
                            dynamicHeight += 20;

                        }
                        dataGridView.Size = new Size(width, height);



                        cboKPI.Location = new Point(70, 3);
                        cboCol.Location = new Point(325,3);
                        panelCol.Location = new Point(325, 10);
                        lblKPI.Location = new Point(0, 5) ;
                        lblCol.Location = new Point(240, 5);



                        btnValidate.Location = new Point(1000, 1);
                        btnValidateAll.Location = new Point(1080, 1);
                        btnValidateKPI.Location = new Point(1200, 1);



                        cboCol.MaxDropDownItems = 1;

                        panelCol.Visible = false;



                        cboCol.KeyPress += (s, e) =>
                            {
                                cboCol.DroppedDown = false;
                            };

                        cboCol.Click += (s, e) =>
                            {
                                panelCol.Visible = true;
                                cboCol.AutoCompleteMode = AutoCompleteMode.None;
                            };


                        panelCol.MouseLeave += (s, e) =>
                        {
                            panelCol.Visible = false;
                        };

                        tp.Click += (s, e) =>
                            {
                                panelCol.Visible = false;
                            };


                        dataGridView.Click += (s, e) =>
                            {
                                panelCol.Visible = false;
                            };



                        dataGridView.Location = new Point(0, 30);

                        btnValidate.Click += (s, e) =>         //btnValidate Event
                        {

                        };


                        btnValidateAll.Click += (s, e) =>        //btnValidateKPI Event
                         {
                            if (cboKPI.Text == null || cboKPI.Text == "" )
                            {
                                MessageBox.Show("Select a KPI!", "No KPI Selected!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            else
                            {
                                Column_Validating(dataGridView, cboKPI.SelectedItem.ToString());
                            }
                         };



                        btnValidateKPI.Click += (s, e) =>        //btnValidateKPI Event
                           {
                               Validate_KPI(dataGridView);
                           };

                        }
                    }
               }
           }

       catch (Exception e)
       {
           MessageBox.Show(e.Message);
       }
    }

Can someone help me stop from adding more values on int dynamicHeight once it changes tabpages?




Aucun commentaire:

Enregistrer un commentaire