Hi dnnyobi,
You need to save control data in database and on formload you have to fetch record and make loop in datatable accordingly to count of your datatable rows it will add automatically controls. for more understanding refer below code.
Namespaces
C#
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Code
C#
string constr = "";
private void btnAdd_Click(object sender, EventArgs e)
{
DynamicAddControls(" ");
}
private void btnDelete_Click(object sender, EventArgs e)
{
Button button = (sender as Button);
int index = int.Parse(button.Name.Split('_')[1]);
panel1.Controls.Remove(panel1.Controls.Find("txt_" + index, true)[0]);
panel1.Controls.Remove(button);
foreach (Button btn in panel1.Controls.OfType<Button>())
{
int controlIndex = int.Parse(btn.Name.Split('_')[1]);
if (controlIndex > index)
{
TextBox txt = (TextBox)panel1.Controls.Find("txt_" + controlIndex, true)[0];
btn.Top = btn.Top - 25;
txt.Top = txt.Top - 25;
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
string messageofTextBox = "";
foreach (TextBox dynamictext in panel1.Controls.OfType<TextBox>())
{
messageofTextBox += "TextBox " + dynamictext.Name + " value is " + dynamictext.Text + "\n";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("INSERT INTO tblTestValue(TextBoxId, TextBoxValues) VALUES(@TextBoxId,@TextBoxValues)", con);
cmd.Parameters.AddWithValue("@TextBoxId", dynamictext.Name);
cmd.Parameters.AddWithValue("@TextBoxValues", dynamictext.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT TextBoxId, TextBoxValues FROM tblTestValue", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
DynamicAddControls(dt.Rows[i][""].ToString());
}
}
private void DynamicAddControls(string id)
{
TextBox textbox = new TextBox();
int count = panel1.Controls.OfType<TextBox>().ToList().Count;
textbox.Location = new System.Drawing.Point(10, 25 * count);
textbox.Size = new System.Drawing.Size(80, 20);
if (!string.IsNullOrEmpty(id))
{
textbox.Name = "txt_" + (count + 1);
}
else
{
textbox.Name = id;
}
panel1.Controls.Add(textbox);
Button button = new Button();
button.Location = new System.Drawing.Point(95, 25 * count);
button.Size = new System.Drawing.Size(60, 20);
button.Name = "btnDelete_" + (count + 1);
button.Text = "Delete";
button.Click += new System.EventHandler(this.btnDelete_Click);
panel1.Controls.Add(button);
}
VB.Net
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
DynamicAddControls(" ")
End Sub
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = (TryCast(sender, Button))
Dim index As Integer = Integer.Parse(button.Name.Split("_"c)(1))
panel1.Controls.Remove(panel1.Controls.Find("txt_" & index, True)(0))
panel1.Controls.Remove(button)
For Each btn As Button In panel1.Controls.OfType(Of Button)()
Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_"c)(1))
If controlIndex > index Then
Dim txt As TextBox = CType(panel1.Controls.Find("txt_" & controlIndex, True)(0), TextBox)
btn.Top = btn.Top - 25
txt.Top = txt.Top - 25
End If
Next
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim messageofTextBox As String = ""
For Each dynamictext As TextBox In panel1.Controls.OfType(Of TextBox)()
messageofTextBox += "TextBox " & dynamictext.Name & " value is " + dynamictext.Text & vbLf
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblTestValue(TextBoxId, TextBoxValues) VALUES(@TextBoxId,@TextBoxValues)", con)
cmd.Parameters.AddWithValue("@TextBoxId", dynamictext.Name)
cmd.Parameters.AddWithValue("@TextBoxValues", dynamictext.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT TextBoxId, TextBoxValues FROM tblTestValue", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
For i As Integer = 0 To dt.Rows.Count - 1
DynamicAddControls(dt.Rows(i)("").ToString())
Next
End Sub
Private Sub DynamicAddControls(ByVal id As String)
Dim textbox As TextBox = New TextBox()
Dim count As Integer = panel1.Controls.OfType(Of TextBox)().ToList().Count
textbox.Location = New System.Drawing.Point(10, 25 * count)
textbox.Size = New System.Drawing.Size(80, 20)
If Not String.IsNullOrEmpty(id) Then
textbox.Name = "txt_" & (count + 1)
Else
textbox.Name = id
End If
panel1.Controls.Add(textbox)
Dim button As Button = New Button()
button.Location = New System.Drawing.Point(95, 25 * count)
button.Size = New System.Drawing.Size(60, 20)
button.Name = "btnDelete_" & (count + 1)
button.Text = "Delete"
AddHandler button.Click, New System.EventHandler(AddressOf Me.btnDelete_Click)
panel1.Controls.Add(button)
End Sub
Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim button As Button = CType(sender, Button)
Dim index As Integer = Integer.Parse(button.Name.Split("_")(1))
panel1.Controls.Remove(panel1.Controls.Find(("txt_" & index), True)(0))
panel1.Controls.Remove(button)
For Each btn As Button In panel1.Controls.OfType(Of Button)()
Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_")(1))
If (controlIndex > index) Then
Dim txt As TextBox = CType(panel1.Controls.Find(("txt_" & controlIndex), True)(0), TextBox)
btn.Top = (btn.Top - 25)
txt.Top = (txt.Top - 25)
End If
Next
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim messageofTextBox As String = ""
For Each dynamictext As TextBox In panel1.Controls.OfType(Of TextBox)()
messageofTextBox += "TextBox " & dynamictext.Name & " value is " + dynamictext.Text & vbLf
Dim constr As String = ""
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblTestValue VALUES(@Value)", con)
cmd.Parameters.AddWithValue("@Value", dynamictext.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
MessageBox.Show(messageofTextBox)
End Sub