Click here to Skip to main content
14,699,126 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I working on a window application. I want that when I click on add button then it creates one by one text boxes dynamically. I store the data in the sql server database & show them in a data grid view. So can u help me for add and view the text box values.
Posted
Comments
[no name] 11-May-13 17:15pm
   
What have you tried? What is wrong with the answers that you already have? What , specifically, is it that you need help with? Can you not create a textbox dynamically using the code you have already been given? Can you not connect to whatever database you are using? Are you unable to save information to the database? Be specific.
Nelek 11-May-13 20:48pm
   
Sergey Alexandrovich Kryukov 11-May-13 23:16pm
   
Look at the type "TextBox" and tell us its exact name.
—SA

To create some text boxes dynamically you can add in scope of button_click following code:
private void button1_Click(object sender, EventArgs e)
{
   TextBox tb1 = new TextBox();
   tb1.ID = "textBox1";
   tb1.Text = "Hi";
   yourform.Controls.Add(tb1)
}

Also, try read the following post:
"C# Insert into SQL when button click"
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/71526c3e-66a5-4309-bdc9-9e29064faef6[^]
   
v2
Try:
int x = 20; int y = 20; int margin = 5;
for (int i = 0; i < 10; i++)
   {
   TextBox tb = new TextBox();
   tb.Location = new Point(x, y);
   y += tb.Height + margin;
   Controls.Add(tb);
   }
Will create ten text boxes and display them in a vertical stack. To access them:
foreach( Control c in Controls)
   {
   TextBox tb = c as TextBox;
   if (tb != null)
      {
      ...
      }
   }
Exactly how you save them to your database is up to you - I have no idea of your DB structure, or what you are trying to with the data.
   
Comments
Volynsky Alex 12-May-13 6:15am
   
+5 great 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