Hi dnnyobi,
Refer below sample. code.
You need to store dynamically added data in a table and on form_load you need to fetch record from database to datatable make loop in datatable and accordingly to loop data will be adde dynamically.
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
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)
{
foreach (Button btn in panel1.Controls.OfType<Button>())
{
int controlIndex = int.Parse(btn.Name.Split('_')[1]);
TextBox txt = (TextBox)panel1.Controls.Find("txt_" + controlIndex, true)[0];
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("INSERT INTO tblTestValue(TextBoxId, TextBoxValues,ButonId,ButonValues) VALUES(@TextBoxId,@TextBoxValues,@ButonId,@ButonValues)");
cmd.Parameters.AddWithValue("@TextBoxId", txt.Name);
cmd.Parameters.AddWithValue("@TextBoxValues", txt.Text);
cmd.Parameters.AddWithValue("@ButonId", btn.Name);
cmd.Parameters.AddWithValue("@ButonValues", btn.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,ButonId,ButonValues 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);
if (count == 0)
{
button.Text = "Ok";
}
if (count == 1)
{
button.Text = "Save";
}
if (count == 2)
{
button.Text = "Upload";
}
button.Click += new System.EventHandler(this.btnDelete_Click);
panel1.Controls.Add(button);
}
VB.Net
Private constr As String = ""
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)
For Each btn As Button In panel1.Controls.OfType(Of Button)()
Dim controlIndex As Integer = Integer.Parse(btn.Name.Split("_"c)(1))
Dim txt As TextBox = CType(panel1.Controls.Find("txt_" & controlIndex, True)(0), TextBox)
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblTestValue(TextBoxId, TextBoxValues,ButonId,ButonValues) VALUES(@TextBoxId,@TextBoxValues,@ButonId,@ButonValues)")
cmd.Parameters.AddWithValue("@TextBoxId", txt.Name)
cmd.Parameters.AddWithValue("@TextBoxValues", txt.Text)
cmd.Parameters.AddWithValue("@ButonId", btn.Name)
cmd.Parameters.AddWithValue("@ButonValues", btn.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,ButonId,ButonValues 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)
If count = 0 Then
button.Text = "Ok"
End If
If count = 1 Then
button.Text = "Save"
End If
If count = 2 Then
button.Text = "Upload"
End If
AddHandler button.Click, New System.EventHandler(AddressOf Me.btnDelete_Click)
panel1.Controls.Add(button)
End Sub