Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need insert into command for dinamic button...they are autocreated from Item datagrid

What I have tried:

private void Form1_Load(object sender, EventArgs e)  
       {  
           // TODO: This line of code loads data into the 'bssDataSet.roba_usluge' table. You can move, or remove it, as needed.  
           this.roba_uslugeTableAdapter.Fill(this.bssDataSet.roba_usluge);  
  
  
  
           SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=bss;Integrated Security=True");  
           //SqlCommand cmd = new SqlCommand("SELECT Concat(ime),data FROM roba_usluge", con);  
          // SqlCommand cmd = new SqlCommand("SELECT (redni_broj),data FROM roba_usluge", con);  
           SqlCommand cmd = new SqlCommand("SELECT (ime), data FROM roba_usluge", con);  
          // SqlCommand cmd = new SqlCommand("SELECT Concat(ime, cijena_sa_porezom),data FROM roba_usluge", con);  
  
           // Getting all the data to ItemTable        
           var da = new SqlDataAdapter(cmd);  
           var ItemTable = new DataTable();  
           da.Fill(ItemTable);  
  
           con.Open();  
           Int32 count = ItemTable.Rows.Count;  
           con.Close();  
  
           int top = 50;  
           int left = 50;  
           for (int i = 1; i <= count; i++)  
           // for (int i = 0; i < count; i++)      
           {  
               Button button = new Button();  
               button.Size = new Size(128,128);  
               button.BackColor = Color.Transparent;  
               //button.FlatStyle = FlatStyle.Flat;  
               button.FlatAppearance.BorderSize = 0;  
               button.Font = new System.Drawing.Font("Trebuchet MS", 10);  
               button.TextAlign = ContentAlignment.TopCenter;  
               button.BackgroundImageLayout = ImageLayout.Zoom;  
                 
               button.Left = left;  
               button.Top = top;  
               button.Text = ItemTable.Rows[i - 1][0].ToString();  
               //if (ItemTable.Rows[i - 1][1] != null && ItemTable.Rows[i - 1][1] != "")  
                   if (ItemTable.Rows[i - 1][1] != null)  
                   {  
                   try  
                   {  
                       byte[] _byte = (byte[])ItemTable.Rows[i - 1][1];  
                       MemoryStream ms = new MemoryStream(_byte);  
                       button.BackgroundImage = System.Drawing.Image.FromStream(ms);//bilo image  
                   }  
                   catch { }  
               }  
               // button.Text = ItemTable.Rows[i][0].ToString();      
               this.Controls.Add(button);  
               if (i % 5 == 0)  
               {  
                   left = 50;  
                   top += button.Height + 2;  
               }  
               else  
               {  
                   left += button.Width + 2;  
               }  
               // top += button.Height + 2;      
  
  
           }  
       }  
Posted
Updated 6-Apr-18 19:10pm

1 solution

You're already creating the button in your code. So when you create the button, just attach an event handler to it. You can use the same eventhandler for all the buttons.

Something like

C#
...
Button button = new Button();  
button.Size = new Size(128,128);  
button.BackColor = Color.Transparent;  
button.Click += new System.EventHandler(dynamicbutton_Click);
...

and then in the same class you would have the event handler
void dynamicbutton_Click(object sender, EventArgs e) {
  // do something
}

If you need, you can add some identification to Tag property to distinguish the buttons fro each other once they are clicked. See Control.Tag Property (System.Windows.Forms)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900