Jayanth Reddy

Jayanth Reddy

  • NA
  • 250
  • 9.9k

inserting dynamic textbox’s multiple text values to dataTabl

Sep 18 2019 2:07 AM
I am trying to add a row for data table whenever we click on a button and for n clicks n rows of textboxes properties along with its text values should be added to n datarows of a datatable.Entire code is fine but the run time text values were not getting added.Please help me and thanks in advance
 
(Kindly Have a look at rar file for the output)
  1. namespace DataTableExample  
  2. {  
  3. public partial class Form1 : Form  
  4. {  
  5. public Form1()  
  6. {  
  7. InitializeComponent();  
  8. }  
  9. internal TextBox tb2;  
  10. internal int X = 1;  
  11. internal int A = 1;  
  12. DataTable dt = new DataTable();  
  13. DataRow dr;  
  14. private void Button1_Click(object sender, EventArgs e)  
  15. {  
  16. if (X <13)  
  17. {  
  18. Show();  
  19. X++;  
  20. }  
  21. else  
  22. {  
  23. MessageBox.Show("Only 12 values per page");  
  24. }  
  25. }  
  26. private new void Show()  
  27. {  
  28. tb2 = new TextBox();  
  29. tb2.Multiline = true;  
  30. //tb2.Text = UserControl.;  
  31. tb2.Height = 22;  
  32. tb2.Width = 60;  
  33. tb2.Left = 43;  
  34. tb2.Top = 140 + A* 36;  
  35. this.Controls.Add(tb2);  
  36. A+=1;  
  37. dr = dt.NewRow();  
  38. dr["text"] = tb2.Text;  
  39. dr["Height"] = tb2.Height;  
  40. dr["Width"] = tb2.Width;  
  41. dr["Left"] = tb2.Left;  
  42. dr["Top"] = tb2.Top;  
  43. dt.Rows.Add(dr);  
  44. }  
  45. private void Button2_Click(object sender, EventArgs e)  
  46. {  
  47. dataGridView1.DataSource = dt;  
  48. }  
  49. private void Form1_Load(object sender, EventArgs e)  
  50. {  
  51. dt.Columns.Add("text",typeof(String));  
  52. dt.Columns.Add("Height",typeof(int));  
  53. dt.Columns.Add("Width"typeof(int));  
  54. dt.Columns.Add("Left"typeof(int));  
  55. dt.Columns.Add("Top"typeof(int));  
  56. }  
  57. }  
  58. }  

Attachment: Task2.rar

Answers (4)