I have some problems with dynamic button in c#. I have developed an application for Pos and I need to create group of product but when I click on group button I have to show product sorting by name of product group.
This is my code:
private void Pos_Load(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=programmj;allowuservariables=True");
conn.Open();
string query = string.Format("SELECT * FROM priduct_group");
MySqlCommand cmd = new MySqlCommand(query);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
adapter.Fill(dt);
ds.Tables.Add(dt);
int top = 0;
int left = 4;
foreach (DataRow dr in dt.Rows)
{
Button button = new Button();
button.FlatStyle = FlatStyle.Flat;
button.BackColor = Color.Gold;
button.Text = dr[1].ToString();
button.Font = new Font("Microsoft Sans Serif", 20, FontStyle.Bold);
button.Size = new Size(170, 85);
button.Left = left;
button.Top = top;
panel8.Controls.Add(button); // here
top += button.Height + 2;
button.Click += new System.EventHandler(Button_Click);
}
}
And this is my another code for displaying product
private void Button_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("server=localhost;user id=root;database=programmj;allowuservariables=True");
conn.Open();
string query = string.Format("SELECT * FROM tblartikujt ");
MySqlCommand cmd = new MySqlCommand(query);
DataTable dt = new DataTable();
DataSet ds = new DataSet();
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
adapter.Fill(dt);
ds.Tables.Add(dt);
int top = 0;
int left = 4;
foreach (DataRow dr in dt.Rows)
{
panel5.Dock = DockStyle.Right;
Button button = new Button();
button.Padding = new Padding(20, 3, 20, 3);
button.FlatStyle = FlatStyle.Flat;
button.ForeColor = Color.White;
button.BackColor = Color.Green;
button.Text = dr[3].ToString();
button.Text = dr[10].ToString();
button.Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold);
button.Size = new Size(200, 85);
button.Left = left;
button.Top = top;
panel5.Controls.Add(button); // here
top += button.Height + 2;
}
}