In this post i'm explaining how to Add Controls Dynamically In Winforms Windows Forms Application Using C# And VB.NET
I have used northwind database and Employees table to populate combobox and dataGridView.
Place two buttons on the form to add combobox and datagridview on button click and write below mentioned code in click event of each button respectively.
C#
Here X and Y co-ordinates are used to define at which location the control needs to be placed.
VB.NET
I have used northwind database and Employees table to populate combobox and dataGridView.
Place two buttons on the form to add combobox and datagridview on button click and write below mentioned code in click event of each button respectively.
C#
01private void btnDropDown_Click(object sender, EventArgs e)02 {03 int x = 13, y = 70;04 ComboBox cmbDynamic = new ComboBox();05 cmbDynamic.Location = new System.Drawing.Point(x, y);06 cmbDynamic.Name = "cmbDyn";07 cmbDynamic.DisplayMember = "FirstName";08 cmbDynamic.ValueMember = "EmployeeID";09 cmbDynamic.DataSource = employeesBindingSource;10 Controls.Add(cmbDynamic);11 12 13 }Here X and Y co-ordinates are used to define at which location the control needs to be placed.
01private void btnDataGrid_Click(object sender, EventArgs e)02 {03 int x = 13, y = 100;04 DataGridView gvDynamic = new DataGridView();05 gvDynamic.Location = new System.Drawing.Point(x, y);06 gvDynamic.Name = "gvDyn";07 gvDynamic.Width = 250;08 gvDynamic.Height = 260;09 gvDynamic.DataSource = employeesBindingSource;10 Controls.Add(gvDynamic);11 }VB.NET
01Private Sub btnDropDown_Click(sender As Object, e As EventArgs)02 Dim x As Integer = 13, y As Integer = 7003 Dim cmbDynamic As New ComboBox()04 cmbDynamic.Location = New System.Drawing.Point(x, y)05 cmbDynamic.Name = "cmbDyn"06 cmbDynamic.DisplayMember = "FirstName"07 cmbDynamic.ValueMember = "EmployeeID"08 cmbDynamic.DataSource = employeesBindingSource09 Controls.Add(cmbDynamic)10 11 12End Sub01Private Sub btnDataGrid_Click(sender As Object, e As EventArgs)02 Dim x As Integer = 13, y As Integer = 10003 Dim gvDynamic As New DataGridView()04 gvDynamic.Location = New System.Drawing.Point(x, y)05 gvDynamic.Name = "gvDyn"06 gvDynamic.Width = 25007 gvDynamic.Height = 26008 gvDynamic.DataSource = employeesBindingSource09 Controls.Add(gvDynamic)10End Sub

0 comments:
Post a Comment