This code sample demonstrates how to dynamically create buttons that are created from information from a SQL-Server table that when the button is clicked returns the primary key to the underlying data in the database which permits retrieval of fields in the same table and also data from child tables. How this differs from the majority of samples on the web is that those samples create click events on the fly in a form with no good method to use information for a button.
The following screenshot shows a window’s form after loading a primary key and customer name from a customer table from a modified version of Microsoft’s North Wind database.
On the left the buttons are placed into a panel, to the right of the panel is an invisible DataGridView. Beneath the those two is a Checkbox which is used to toggle between two different methods for displaying data. The first method displays a modal child form that data binds information returned from calling a method from a delegate to return several fields for that record and upon return provides you with any changed data. The changed data could be pushed back to the database table but that is outside the scope of the code sample. Note the primary key is displayed in the modal form's caption so if you care to can validate the right record was returned although that should be apparent from the call to get the data.
Here is the code in the main form that responses from a delegate in the class which created our buttons.
void button_Clicker(object sender, IdentifierButtonEventArgs e) { dataGridView1.Visible = false; dataGridView1.DataSource = null; var Customer = dataOps.GetCustomer(e.Identifier); if (checkBox1.Checked) { customerEditor f = new customerEditor(Customer); try { if (f.ShowDialog() == DialogResult.OK) { MessageBox.Show(f.Customer.ToString()); } } finally { f.Dispose(); } } else { //MessageBox.Show(Customer + $"\nButton Name: {((Button)sender).Name}"); dataGridView1.DataSource = dataOps.GetOrders(e.Identifier); dataGridView1.Visible = true; } }
void button_Clicker(object sender, IdentifierButtonEventArgs e) { dataGridView1.Visible = false; dataGridView1.DataSource = null; var Customer = dataOps.GetCustomer(e.Identifier); if (checkBox1.Checked) { customerEditor f = new customerEditor(Customer); try { if (f.ShowDialog() == DialogResult.OK) { MessageBox.Show(f.Customer.ToString()); } } finally { f.Dispose(); } } else { //MessageBox.Show(Customer + $"\nButton Name: {((Button)sender).Name}"); dataGridView1.DataSource = dataOps.GetOrders(e.Identifier); dataGridView1.Visible = true; } }
public void CreateButtonsFromTable(DataTable sender, string Identifier, string fieldName) { Buttons = new List<Button>(); this.ButtonCount = sender.Rows.Count - 1; sender.AsEnumerable().Select((row) => new { ID = Convert.ToInt32(row[Identifier]), Name = row[fieldName].ToString() }) .ToList().ForEach((item) => { Button b = new Button { Name = string.Concat(ButtonBaseName, item.Name), Text = item.Name, Tag = item.ID, Size = ButtonSize, Location = new Point(25, this.Base), Parent = ParentControl, Visible = true }; b.Click += (object s, EventArgs e) => { ClickedHandler(s, new IdentifierButtonEventArgs(Convert.ToInt32(((Button)s).Tag))); }; this.ParentControl.Controls.Add(b); Buttons.Add(b); Base += BaseAddition; }); }
public void CreateButtonsFromTable(DataTable sender, string Identifier, string fieldName) { Buttons = new List<Button>(); this.ButtonCount = sender.Rows.Count - 1; sender.AsEnumerable().Select((row) => new { ID = Convert.ToInt32(row[Identifier]), Name = row[fieldName].ToString() }) .ToList().ForEach((item) => { Button b = new Button { Name = string.Concat(ButtonBaseName, item.Name), Text = item.Name, Tag = item.ID, Size = ButtonSize, Location = new Point(25, this.Base), Parent = ParentControl, Visible = true }; b.Click += (object s, EventArgs e) => { ClickedHandler(s, new IdentifierButtonEventArgs(Convert.ToInt32(((Button)s).Tag))); }; this.ParentControl.Controls.Add(b); Buttons.Add(b); Base += BaseAddition; }); }
using System; namespace CreateDynamicTextBoxes_CS { public class IdentifierButtonEventArgs : EventArgs { public IdentifierButtonEventArgs(int id) { Identifier = id; } public int Identifier { get; set; } } }
using System; namespace CreateDynamicTextBoxes_CS { public class IdentifierButtonEventArgs : EventArgs { public IdentifierButtonEventArgs(int id) { Identifier = id; } public int Identifier { get; set; } } }
public class Customer { public int CustomerIdentifier { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public override string ToString() { return $"{CompanyName}, {ContactName}, {ContactTitle}"; } }
public class Customer { public int CustomerIdentifier { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public override string ToString() { return $"{CompanyName}, {ContactName}, {ContactTitle}"; } }
CreateButtons MakeButtons = new CreateButtons() { ParentControl = panel2, Base = 10, ButtonBaseName = "btn", BaseAddition = 60, ButtonSize = new Size(100, 50) };
CreateButtons MakeButtons = new CreateButtons() { ParentControl = panel2, Base = 10, ButtonBaseName = "btn", BaseAddition = 60, ButtonSize = new Size(100, 50) };