I use dinamycal button and add TableLayoutPanel on button Now need to use labels from that Table layout on my button My code for button click
void btn_Click(object sender, EventArgs e) { Button btn = (Button)sender; Label Label = (Label)sender; // here need to use priceLabel and nameLabel var itemAndPrice = btn.Text; var item = Label.Text; int sno = dataGridView1.Rows.Count + 1; string nazivartikla = item[0].ToString(); string exists = ""; foreach (DataGridViewRow g1 in dataGridView1.Rows) { if (g1.Cells[2].Value.ToString().Trim() == item[0].ToString().Trim()) { exists = "y"; g1.Cells[3].Value = Convert.ToInt32(g1.Cells[3].Value) + 1; } } if (exists != "y") { dataGridView1.Rows.Add(sno, SifraTextBox.Text, nazivartikla, 1); } dataGridView1.Refresh(); }
This part of code is ok
private void TopliNapitciButton_Click(object sender, EventArgs e) { panel1.Controls.Clear(); string conString = ConnectionClass.PullData(labelgodina.Text); SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand("SELECT naziv, FORMAT(cijena_sa_porezom, 'N2') AS cijena, data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con); // SqlCommand cmd = new SqlCommand("SELECT naziv+' - '+CAST(FORMAT(cijena_sa_porezom, 'N2') AS varchar), data FROM dbo.roba_usluge where grupa_artikala='Napitci' and status='Aktivan'", con); var da = new SqlDataAdapter(cmd); var ItemTable = new DataTable(); da.Fill(ItemTable); con.Open(); Int32 count = ItemTable.Rows.Count; con.Close(); int top = 190; int left = 5; for (int i = 0; i < count; i++) { Button button = new Button(); button.Size = new Size(128, 158); button.BackColor = Color.Transparent; button.FlatAppearance.BorderSize = 0; button.Font = new System.Drawing.Font("Roboto", 10); button.TextAlign = ContentAlignment.BottomCenter; button.BackgroundImageLayout = ImageLayout.Zoom; // Create TableLayoutPanel TableLayoutPanel table = new TableLayoutPanel(); table.Dock = DockStyle.Fill; table.RowCount = 3; table.RowStyles.Add(new RowStyle(SizeType.Percent, 70F)); table.RowStyles.Add(new RowStyle(SizeType.Percent, 15F)); table.RowStyles.Add(new RowStyle(SizeType.Percent, 15F)); // Image PictureBox pictureBox = new PictureBox(); pictureBox.Dock = DockStyle.Fill; pictureBox.SizeMode = PictureBoxSizeMode.StretchImage; if (ItemTable.Rows[i]["data"] != DBNull.Value) { byte[] _byte = (byte[])ItemTable.Rows[i]["data"]; MemoryStream ms = new MemoryStream(_byte); pictureBox.Image = Image.FromStream(ms); } // Label for name Label nameLabel = new Label(); nameLabel.Dock = DockStyle.Fill; nameLabel.TextAlign = ContentAlignment.MiddleCenter; nameLabel.Font = new System.Drawing.Font("Roboto", 12, FontStyle.Bold); nameLabel.ForeColor = System.Drawing.Color.Gray; nameLabel.Text = ItemTable.Rows[i]["naziv"].ToString(); // Label for price Label priceLabel = new Label(); priceLabel.Dock = DockStyle.Fill; priceLabel.TextAlign = ContentAlignment.MiddleCenter; priceLabel.ForeColor = System.Drawing.Color.DarkOrange; priceLabel.Font = new System.Drawing.Font("Roboto", 12, FontStyle.Bold); priceLabel.Text = ItemTable.Rows[i]["cijena"].ToString() + " KM"; // Add controls to TableLayoutPanel table.Controls.Add(pictureBox, 0, 0); table.Controls.Add(nameLabel, 0, 1); table.Controls.Add(priceLabel, 0, 2); button.Left = left; button.Top = top; // Add TableLayoutPanel to button button.Controls.Add(table); button.Click += new EventHandler(this.btn_Click); panel1.Controls.Add(button); if ((i + 1) % 4 == 0) { left = 5; top += button.Height + 2; } else { left += button.Width + 2; } }