3

I'm generating the columns of a gridview in code which is working fine (I have AutoGenerateColumns="false" set on the page), so I don't have any columns defined in the html markup.

I would like to make one of the columns a ButtonField, so I can use a RowCommand handler in the code - is there a way to make a column a ButtonField programmatically?

1 Answer 1

5

ButtonField programmatic adding:

var buttonField = new ButtonField
                      {
                          ButtonType = ButtonType.Button,
                          Text = "My button",
                          CommandName = "DoSomething",
                      };
Grid.Columns.Add(buttonField);

Markup:

<asp:GridView runat="server" ID="Grid" AutoGenerateColumns="false" OnRowCommand="RowCommandHandler"></asp:GridView>

Handler:

protected void RowCommandHandler(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DoSomething")
    {
        // place code here
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.