Tania Emmerechts

Tania Emmerechts

  • NA
  • 4
  • 33.3k

dynamic creating and deleting controls

Oct 30 2010 7:10 AM

Hi,
I create on a form dynamicly labels wit "Product" and "vendor" and a delete button.
This happens when the event form.Activated happens.
When the user pushes the "delete" button, the row with the "product" and the "vendor" has to be deleted from the database and the screen.
Everything works fine except the deleting from the screen.
I thought when the form.Activated was fired, it would refresh my lines, but it doesn't.
What do I have to do??
This is my code to create the lines :  
    private void NewProject_Activated(object sender, EventArgs e)
        {

            #region ophalen producten en vendors
            int projectID = Globals.ProjectID;
            //Bepaal ConnectionString
            string connectioncbProduct = DatabaseFuncties.ConnectionString;

            //Bepaal connectie en open ze
            SqlConnection connection = new SqlConnection(connectioncbProduct);
            connection.Open();

            //Selecteer de producten en vendors voor het project
            string commandTextcbProduct = ("select Products.ProductName,  Vendors.VendorName, ProjectProducts.ProjectProductsID  " +
                                            "from Products " +
                                            "inner join ProjectProducts " +
                                            "on Products.ProductID = ProjectProducts.ProductID " +
                                            "inner join Vendors " +
                                            "on Vendors.VendorID = ProjectProducts.VendorID " +
                                            "where ProjectProducts.ProjectID = " + projectID );

            //Maak een nieuw object
            SqlDataAdapter dataAdaptercbProduct = new SqlDataAdapter(commandTextcbProduct, connectioncbProduct);

            // vul de dataset
            DataSet dataSetcbProduct = new DataSet();
            dataAdaptercbProduct.Fill(dataSetcbProduct);
            connection.Close();
           
            //Bepaal de datatable
            DataTable dataTablecbProduct = dataSetcbProduct.Tables[0];

            //Doorloop alle regels en toon de producten en vendors
            int yPoint = 528;

            foreach (DataRow dataRowcbProduct in dataTablecbProduct.Rows)
            {
                {
                    Label labelProduct = new Label();
                    {
                        labelProduct.Location = new Point(33, yPoint);
                        labelProduct.Text = Convert.ToString(dataRowcbProduct["ProductName"]);
                        labelProduct.AutoSize = true;
                    }

                    Label labelVendor = new Label();
                    {
                        labelVendor.Location = new Point(500, yPoint);
                        labelVendor.Text = Convert.ToString(dataRowcbProduct["VendorName"]);
                        labelProduct.AutoSize = true;
                    }

                    Button buttonDelete = new Button();
                    {
                        buttonDelete.Text = "Delete";
                        buttonDelete.Location = new Point(750, yPoint);
                        buttonDelete.Size = new Size (46,20);
                        buttonDelete.Tag = Convert.ToString(dataRowcbProduct["ProjectProductsID"]);
                        buttonDelete.Name = "cbDelete";
                        buttonDelete.Click +=new EventHandler(buttonDelete_Click);
                    }

                    this.Controls.Add(labelProduct);
                    this.Controls.Add(labelVendor);
                    this.Controls.Add(buttonDelete);
             
                   
                    yPoint = yPoint + 22;
                }
            }

            #endregion
        }

This is my code to delete the lines :
   private void buttonDelete_Click(object sender, System.EventArgs e)
        {
          if (sender is Button)
          {
            Button buttonDelete = sender as Button;
            string projectProductID = Convert.ToString (buttonDelete.Tag);
           // MessageBox.Show(tag);

            //Bepaal ConnectionString
            string connectionstring = DatabaseFuncties.ConnectionString;

            //Bepaal connectie en open ze
            SqlConnection connection = new SqlConnection(connectionstring);
            connection.Open();

            //Selecteer de producten en vendors voor het project
            string commandTextcbProduct = ("DELETE FROM ProjectProducts " +
                                            "WHERE ProjectProducts.ProjectProductsID = " + projectProductID);

            //Maak een nieuw object
            SqlDataAdapter dataAdaptercbProduct = new SqlDataAdapter(commandTextcbProduct, connectionstring);

            try
            {
                dataAdaptercbProduct.DeleteCommand = connection.CreateCommand();
                dataAdaptercbProduct.DeleteCommand.CommandText = commandTextcbProduct;
                dataAdaptercbProduct.DeleteCommand.ExecuteNonQuery();
                MessageBox.Show("Product deleted");
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            connection.Close();
            this.Activate();
          }
        }

Answers (1)