简体   繁体   中英

How to add a button control dynamically into a GridView cell ? VB.NET

Look, it´s simple. I have a GridView which is populated with data from my database.

What I want is put a button in each cell that contains a specific datum.

Pls, look the image below. It describes exactly what I want

In VB.NET, pls! =)

Example Image

Thanks very much!

You are going to need to define the button control inside of the <Columns> section of your GridView markup, like this:

<asp:gridview id="CustomersGridView" runat="server">
    <columns>
        <asp:boundfield datafield="DateColumn" headertext="Date"/>
        <asp:TemplateField>
            <HeaderTemplate>
                Positive
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label id="LabelPositive" runat="server" Text='<%# Eval("PositiveColumn")%>' />
                <br />
                <asp:Button id="ButtonPositive" runat="server" Text="Show" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Negative
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label id="LabelNegative" runat="server" Text='<%# Eval("NegativeColumn")%>' />
                <br />
                <asp:Button id="ButtonNegative" runat="server" Text="Show" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <HeaderTemplate>
                Neutral
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label id="LabelNeutral" runat="server" Text='<%# Eval("NeutralColumn")%>' />
                <br />
                <asp:Button id="ButtonNeutral" runat="server" Text="Show" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:boundfield datafield="NoCommentsColumn" headertext="No Comments"/>
        <asp:boundfield datafield="TotalColumn" headertext="Total"/>
    </columns>
</asp:gridview>

Note: The datafield and Eval() calls are bound to made up names like NeutralColumn and NoCommentsColumn , substitute those names with your real database field names.

You need to add while data binding the grid. Look for an event call and handle your code there.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM