Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for (int i = 0; i < count; i++)
{
string Qry = "Select Category from tblProduct";
OleDbDataAdapter adpter = new OleDbDataAdapter(Qry, objCon);
DataTable dt = new DataTable();
adpter.Fill(dt);

Button b = new Button();
b.Name = dt.Rows[i]["Category"].ToString();
b.Text = dt.Rows[i]["Category"].ToString();
b.Click += new EventHandler(btnRetreive_Click);

b.Location = new Point(20, (i * 20));
this.Controls.Add(b);
//you will also need to add some code to accurately size and place the button. You can
//do that based on the amount of controls there are with dtButtonsToCreate.Count
}
Posted
Comments
ganesh89_babu 28-Sep-13 6:44am    
i need to change the btnRetreive_click to another name? Plz anyone reply
[no name] 28-Sep-13 7:03am    
Why would you need to this at all? Just use the same event handler for all of your buttons. The "sender" parameter for the event is the button that was clicked. So why is that you would need separate event handlers that you would have to code up anyway?
ganesh89_babu 28-Sep-13 7:05am    
ok, then if i want to call that button name in another event how could have call?
[no name] 28-Sep-13 7:13am    
"call that button name" makes no sense at all and you would not need to do anything with the button anyway because if you had actually read my comment, the sender parameter for your event handler is the button that was clicked.

1 solution

You can't. If you think about it, it doesn't make sense to try: Because the name of the event handler needs to be known at compile time, and you need it to change at run time, you would have to set up an virtually infinite list of event handlers in order to cope with whatever data comes back from your dataTable.

Instead, use the sender parameter of the event handler to decide which button was clicked:
C#
private void btnRetrieve_Click(object sender, EventArgs e)
   {
   Button b = sender as Button;
   if (b != null)
      {
      switch (b.Text)
          {
          case "Category 1":
          ...
          }
      }
   }
Or use the Control.Tag property to put a specific value into it for processing in the handler method.
 
Share this answer
 
Comments
ganesh89_babu 28-Sep-13 7:14am    
thanks a lot
OriginalGriff 28-Sep-13 7:26am    
You're welcome!
ganesh89_babu 28-Sep-13 7:20am    
i am keep on doing if i have any doubt i will ask your help ok

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