0

I'm creating a button every row using DataRow. I can't understand if is possible to render a button on a datarow?

 for (int day = 0; day < days.Count; day++)
                        {
                            // dataRow[string.Format("Day{0}", day + 1)] = days[day].Price;

                            // TODO: Store to hidden field.

                            var button = new Button();

                            button.ID = string.Format("{0}", day);
                            button.Attributes.Add("style", "display:block; padding:4px; width:100%; height:100%;");

                            dataRow[day + 1] = GridViewPricing.Controls.Add(button);
                        }

Alternetive:

I create a string html tags but it just render as string on the gridview.

2
  • If you want to add buttons to Rows of GridView, use TemplateField of GridView... Commented Mar 28, 2019 at 3:07
  • You don't add controls directly to GridView in asp. You need to reach at the Cell level and then use AddControl. Commented Mar 28, 2019 at 5:20

2 Answers 2

1

The best practice to do this using web forms as the following :

Disable auto generate columns of grid using AutoGenerateColumns="False" then define the columns manually to fit your requirements like the example below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="LabelName" runat="server" Text='<%#Eval("Name") %>' </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
</asp:GridView>

Then in code behind you can indicate what is the primary key of clicked column , something like this :

protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;

    if (gvr.RowType == DataControlRowType.DataRow)
    {
        string Namme = (gvr.FindControl("LabelName") as Label).Text;
        //Write Query here to Delete Data
    }

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

Comments

0
<asp:GridView ID="GridViewName" runat="server" AutoGenerateColumns="false" OnRowCommand="RowCommandEvent">
<Columns>
.
.
rest of the columns
    <asp:TemplateField HeaderText="ColumnHeader">
        <ItemTemplate>
            <asp:LinkButton ID="ButtonName" runat="server" CommandName="Edit" CommandArgument='<%#Eval("ID") %>'>Edit</asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

you can add a button from a design side, using this you can get the button in every row of the grid.

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.