Hi,
To solve your problem, I suggest to use the dataGridView component of the windows form.
You will dynamically add the button while you populate the component.
You can add or not, depends on the conditional
private void FillData()
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("number", typeof(string));
dt.Columns.Add("value", typeof(string));
DataRow dr = dt.NewRow();
dr["name"] = "Peter";
dr["number"] = "1";
dr["value"] = "0";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "Mary";
dr["number"] = "2";
dr["value"] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "John";
dr["number"] = "3";
dr["value"] = "0";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "Sarah";
dr["number"] = "4";
dr["value"] = "1";
dt.Rows.Add(dr);
dataGridView1.Columns.Add("name", "Name");
dataGridView1.Columns.Add("number", "Number");
dataGridView1.Columns.Add("button", "View");
string[] lst_dados = new string[2];
foreach(DataRow dr1 in dt.Rows)
{
lst_dados[0] = dr1["name"].ToString();
lst_dados[1] = dr1["number"].ToString();
this.dataGridView1.Rows.Add(lst_dados);
if (dr1["value"].ToString() == "1")
{
DataGridViewButtonCell btn = new DataGridViewButtonCell();
btn.Value = "View";
btn.UseColumnTextForButtonValue = true;
this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 2].Cells["button"] = btn;
}
}
}
.
To fire the button, use
CellClick events.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.dataGridView1.Columns["button"].Index)
{
}
}