Ping Lee

Ping Lee

  • NA
  • 11
  • 2.4k

c# Dynamic button to difference event

Jun 10 2019 10:30 PM

I'm setting an MYSQL query for Dynamic button, I want to set click a button to load another table

I had tried  inside foreach

  1. button.Name = dr[0].ToString();  
and for loop
  1. for (int i =1; i<10;i++)  
but not work
  1.     foreach (DataRow dr in dt.Rows)  
  2.     {  
  3.         Button button = new Button();  
  4.         button.FlatStyle = FlatStyle.Flat;  
  5.         button.BackColor = Color.Gold;  
  6.         button.Text = dr[1].ToString(); // Load item name   
  7.         button.Name = dr[0].ToString(); //Load item PK  
  8.         button.Font = new Font("Microsoft Sans Serif", 25, FontStyle.Bold);  
  9.         button.Size = new Size(300, 50);  
  10.         button.Left = left;  
  11.         button.Top = top;  
  12.         panel1.Controls.Add(button); // here  
  13.         top += button.Height + 2;  
  14.           
  15.     }  
  16. }  
  17.   
  18. private void madeReq_frm_Load(object sender, EventArgs e)  
  19. {  
  20.     AddButtons();  
  21. }  
  22.   
  23.   
  24. private void C00001_Click() //DB TABLE PK NAME C00001  
  25. {  
  26.   
  27.     MessageBox.Show("Load DataBase  1");  
  28.   
  29. }  
How can i to fix ? Thank you 

Answers (2)