1

I am currently looking for a way to insert a button on each row of a table (please note this is a dynamic table that will change on a regular basis), this button would be used for deleting the row from the table - I have already tried adding a button on each row using this method:

            foreach (string instrument in splitInstrumentList)
            {
                TableRow r = new TableRow();
                r.Cells.Add(new TableCell());
                Button deleteButton = new Button();

                string instrumentString = instrument.ToString();

                if (instrumentString.Contains(","))
                {
                    instrumentString.Replace(",", string.Empty);
                }

                if (instrumentString.Length > 0 && string.IsNullOrEmpty(instrumentString))
                {
                    r.Cells[0].Text = instrumentString;

                    this.instrumentTable.Rows.Add(r);
                    deleteButton.ID = "deleteButton";
                    deleteButton.Text = "Delete";
                    instrumentTable.Controls.Add(deleteButton);

                }
            }

However I can not do this due to the fact Table can't use the child type Button which I should of realised anyway..

1 Answer 1

2

You need to add the button in a Row's cell, currently you are adding the button to the table itself. You should create a new Cell and then add the Button to the cell , later add the cell to the row like.

TableCell cell = new TableCell();
cell.Controls.Add(deleteButton);
r.Cells.Add(cell);

You should also register an event against the Button Click event before adding it to the cell, where you would perform the delete operation.

Sign up to request clarification or add additional context in comments.

3 Comments

I was planning on doing that after! Thank you though :)
@HelloWorld, planning to do what ? You should create and add the button to the cell in your loop.
Register an event for the buttons click, I just missed that out from this part of the code. Thank you very much though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.