2017-01-25 3 views
0

私がしたかったのは、テーブル内のすべての従業員のボタンを生成することでした。たとえば、テーブルに4人の従業員がいるとしましょう。「支払う」という4つのボタンが必要です。希望の出力のスクリーンショットが含まれています。 私はちょうどこれを行うための任意のアイデアを思い付くことができませんでした...誰も助けてくださいまたは任意の提案。 ありがとうございます。 私はC#とVisual Studioを使用していますenter image description hereテーブル内のすべての要素のボタンを生成する方法は?

+0

[フォームにボタンを動的に追加する方法は?](http://stackoverflow.com/questions/8608311/how-to-add-buttons-dynamically-to-my-form) –

+0

内部で行うあなたが今まで何をしていたのか、従業員を表示しているループ? –

+0

DataGridViewButtonColumn https://msdn.microsoft.com/en-us/library/bxt3k60s.aspx – Serg

答えて

0

あなたは擬似コード以下のようなものを行うことができます。

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

*従業員は、あなたは、彼らがフォーム上にきれいに表示されるようにラベルとボタンの位置を設定する必要があります* Employee型 の集まりであると仮定します。

1

WinForms(?)を使用していると仮定して、DataGridViewコントロールの使用を検討しましたか?目的に合った列タイプのDataGridViewButtonColumnがあります。フォームを作成し、DataGridViewコントロールをその上にドロップし、このデモコードを試してください:

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); 
     } 
    } 
} 
0

これは簡単に実行できます。このようなものを試してみてください。

 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 }); 
      } 
     } 

あなたの従業員の表をループし、必要なコントロールを追加してください。

関連する問題