surafel
surafel

Reputation: 153

How to generate a button for every element in a table?

So what i wanted to do is that to generate a button for every employee in a table. For example let say i have four employees in my tables, so there should be four button saying 'pay' i have included a screenshot of the desired output. I just couldnt come up with any idea to do this...can anyone help please or any suggestions. Thanks in advance. I am using C# and visual studioenter image description here

Upvotes: 0

Views: 465

Answers (3)

Ariful Islam
Ariful Islam

Reputation: 674

You can do this easily. Try something like this-

        private void Form1_Load(object sender, EventArgs e)
        {
            var employees = new string[] { "Emp1", "Emp2", "Emp3", "Emp4" }; 
            int btnTop = 0, btnLeft = 100, lblTop = 0, lblLeft = 20;

            foreach (var employee in employees)
            {
                btnTop += 30; lblTop += 30;
                this.Controls.Add(new Label { Text = employee, Left = lblLeft, Top = lblTop, Width = 50 });
                this.Controls.Add(new Button { Text = "Pay", Left = btnLeft, Top = btnTop, Width = 50 });
            }
        }

Loop through your employee table and add whatever control you want.

Upvotes: 0

Nick Allan
Nick Allan

Reputation: 406

Assuming you are using WinForms(?), have you considered using the DataGridView control? There is a column type of DataGridViewButtonColumn which would suit your purposes. Create a form, drop a DataGridView control onto it and try this demo code:

using System;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.DataGridViewButtonColumn ButtonColumn;
        private System.Windows.Forms.DataGridViewTextBoxColumn EmployeeColumn;

        public Form1()
        {
            //Add a DataGridView control to your form, call it "dataGridView1"
            InitializeComponent();

            EmployeeColumn = new System.Windows.Forms.DataGridViewTextBoxColumn()
            {
                Name = "Employee"
            };

            ButtonColumn = new System.Windows.Forms.DataGridViewButtonColumn()
            {
                Text = "Pay"
            };

            dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { EmployeeColumn, ButtonColumn });

            //Populate this as required
            var oDataTable = new DataTable();
            oDataTable.Columns.Add("Employee", typeof(String));

            dataGridView1.Rows.Add("Tom", ButtonColumn.Text);
            dataGridView1.Rows.Add("Dick", ButtonColumn.Text);
            dataGridView1.Rows.Add("Harry", ButtonColumn.Text);
        }
    }
}

Upvotes: 1

FIre Panda
FIre Panda

Reputation: 6637

You could do something like below pseudo code;

foreach(Employee emp in Employees)
{
  this.Controls.Add(//add label here with unique id)
  this.Controls.Add(//add button here with unique id)
}

*Lets assume Employees is a collection of type Employee * You may have to set location of label and button so that they appear nicely on the form.

Upvotes: 0

Related Questions