Help. I want to create Dynamic buttons inside a stackpanel, I think I have a problem in my code This is my code:
using (MySqlConnection conn = new MySqlConnection(constr))
{
conn.Open();
using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT GarmentName FROM tblthesis", conn))
{
ds = new DataSet();
adapter.Fill(ds);
foreach (DataRow dataRow in ds.Tables[0].Rows)
{
Button button = new Button ();
button.Content = dataRow[0].ToString();
WindowTry winwin = new WindowTry();
winwin.sp.Children.Add(button);
winwin.Show();
Note, (sp) is the stackpanel of my windowTry window.
I have 5 datas in my database wherein I used that to name my buttons, but when I try to put that In WindowTry, and run it. It creates 5 windows with 1 button each, what should I do? please help.
You are creating for each
DataRow
a window, add a button to it, and show it, so it works just as it's implemented. You need to create the window before the loop, and show it after it.