SearchForKnowledge
SearchForKnowledge

Reputation: 3751

How to populate a GridView column with a button

I have the following GridView:

<asp:GridView ID="gvDispMsg" ClientIDMode="Static" ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" EmptyDataText="There is no data to display" runat="server" AllowPaging="false" AllowSorting="true">
    <Columns>
        <asp:BoundField HeaderText="Delete Message" HeaderStyle-Width="12%" />
        <asp:BoundField HeaderText="ID" HeaderStyle-Width="12%" DataField="ID" />
        <asp:BoundField HeaderText="Date" HeaderStyle-Width="13%" DataField="Created" />
        <asp:BoundField HeaderText="Message" HeaderStyle-Width="45%" DataField="Message" />
        <asp:BoundField HeaderText="Active" HeaderStyle-Width="10%" DataField="Active" />
        <asp:BoundField HeaderText="Created By" HeaderStyle-Width="20%" DataField="CreatedBy" />
    </Columns>
</asp:GridView>

It is populated from a SQL table which is executed from code-behind. The only column which is not populated is the Delete Message column.

How can I add a button for each row in the Delete Message column which takes the argument for the ID datafield?

Upvotes: 0

Views: 945

Answers (1)

fubo
fubo

Reputation: 46005

Add

<asp:GridView AutoGenerateDeleteButton="true" DataKeyNames="ID" OnRowDeleted="NewEvent"

and handle the NewEvent in CodeBehind

or

add a <asp:ButtonField ButtonType="Button" CommandName="delete" Text="Delete" HeaderText="Delete"/> instead of a BoundField

or

add

<asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </ItemTemplate> 
    <EditItemTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </EditItemTemplate>
    <FooterTemplate>
        <asp:Button ID="lbiDelete" runat="server" CommandName="Delete" CommandArgument='<%# Bind("ID") %>' />
    </FooterTemplate>                  
</asp:TemplateField>

where you have most abilities to configure this column in every state

Upvotes: 1

Related Questions