I am creating a C# application which fetches data from a database, and dynamically creates 5 textBoxes and one button in a single row.
The number of rows present in the database equals the number of rows of textBoxes and buttons that are created.
I could successfully create the rows of textBoxes and buttons, the textBoxes are even capable of displaying data that is being fetched from the database.
My trouble however is that the button that is generated, does nothing when clicked, now that is not unexpected since i haven't created a handler to handle the click event. But i am confused on how to dynamically create the click even handler for the buttons that are again generated dynamically.
Below is the code sample that generated the textBoxes and buttons.
for (int i = 3; i <= count; i++)
{
com.Parameters[0].Value = i;
using (SqlCeDataReader rd = com.ExecuteReader())
if (rd.Read())
{
pname = (rd["pname"].ToString());
cname = (rd["cname"].ToString());
budget = (rd["budget"].ToString());
advance = (rd["advance"].ToString());
ddate = (rd["ddate"].ToString());
TextBox tobj = new TextBox();
tobj.Location = new Point(10, (40 + ((i - 2) * 20)));
tobj.Tag = 1;
tobj.Text = pname;
tobj.AutoSize = false;
tobj.Width = 150;
tobj.ReadOnly = true;
this.Controls.Add(tobj);
TextBox tobj1 = new TextBox();
tobj1.Location = new Point(160, (40 + ((i - 2) * 20)));
tobj1.Tag = 2;
tobj1.Text = cname;
tobj1.AutoSize = false;
tobj1.Width = 150;
tobj1.ReadOnly = true;
this.Controls.Add(tobj1);
TextBox tobj2 = new TextBox();
tobj2.Location = new Point(310, (40 + ((i - 2) * 20)));
tobj2.Tag = 3;
tobj2.Text = budget;
tobj2.AutoSize = false;
tobj2.Width = 100;
tobj2.ReadOnly = true;
this.Controls.Add(tobj2);
TextBox tobj3 = new TextBox();
tobj3.Location = new Point(410, (40 + ((i - 2) * 20)));
tobj3.Tag = 4;
tobj3.Text = advance;
tobj3.AutoSize = false;
tobj3.Width = 100;
tobj3.ReadOnly = true;
this.Controls.Add(tobj3);
TextBox tobj4 = new TextBox();
tobj4.Location = new Point(510, (40 + ((i - 2) * 20)));
tobj4.Tag = 5;
tobj4.Text = ddate;
tobj4.AutoSize = false;
tobj4.Width = 100;
tobj4.ReadOnly = true;
int due = 0;
due = int.Parse(ddate);
if (due < 5)
{
tobj4.BackColor = System.Drawing.Color.Red;
}
this.Controls.Add(tobj4);
Button button = new Button();
button.Left = 620;
button.Tag = i;
button.Height = 20;
button.Text = "Details";
button.Top = (40 + ((i - 2) * 20));
this.Controls.Add(button);
}
}
Please give me some ideas on how to generate the click event handler.
sql-server?