Click here to Skip to main content
15,889,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written the code below that populates a table dynamically from an array in c#. I want to add the last column which will have buttons at the end of each row. On the button click, it should delete that row.

Please help.

C#
string[] ValuesT = new string[] { code, name, age};                               
TableRow tRowT = new TableRow();
Table1.Rows.Add(tRowT);
for (int T = 0; T < 3; T++)
{
TableCell tCellT = new TableCell();
tCellT.Text = "" + ValuesT[T] + "";
tRowT.Cells.Add(tCellT); 
}


What I have tried:

I added this code to create the buttons but all it does is to replace the first four cells with buttons.
C#
foreach (TableRow row in Table1.Rows)
{
foreach (TableCell cell in row.Cells)
{
Button btn = new Button();
btn.Text = "Delete";
btn.Click += new EventHandler(BtnDelete_Click);
cell.Controls.Add(btn);
}
}
Posted
Updated 2-May-17 2:57am
v2
Comments
[no name] 2-May-17 7:44am    
what issue/error you are facing?

1 solution

foreach (TableRow row in Table1.Rows)
{
    TableCell btnCell = new TableCell();

    Button btn = new Button();
    btn.Text = "Delete";
    btn.Click += new EventHandler(BtnDelete_Click);
    btnCell.Controls.Add(btn);

    row.Cells.Add(btnCell);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900