views:

345

answers:

2

I've created a Gridview control that pulls some data from my database, but doesn't display all of the information I'm hoping it gathers. I choose not to display the primary key for user interface purposes, but require that key for basic operations on the grid.

I'm currently attempting to add a "Delete" option to the table, but unfortunately I'm having very little luck. Here's my .aspx file.

 <asp:GridView ID="ManagerList_GV" runat="server"
                AutoGenerateColumns="False" AllowSorting="True"
                DataKeyNames = "ManagerID" OnRowEditing="editManager"
                OnRowDataBound="FormatManagers" CellPadding="3" >
                <Columns>
                    <asp:BoundField HeaderText="Manager" DataField="FullName" />
                    <asp:BoundField HeaderText="Status" DataField="Status" />
                    <asp:CommandField EditText="Edit" ShowEditButton="True" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton Text="Delete" runat="server" CommandName="deleteRow" />
                        </ItemTemplate>
                    </asp:TemplateField>            
                </Columns>
            </asp:GridView>

Unfortunately, I'm not sure what to do for my back-end. I've created my stored procedure and business layer subroutine, but I'm not sure how to a) pull in which row was just clicked and then b) access the primary key with respect to this row, since I'm not displaying it (the primary key is ManagerID).

Thanks in advance for your help :)

A: 

Add a BoundColumn displaying the Primary Key as visible on design view but after databinding make the column with the PK set to visible false.

e.g.

<Columns>                    
        <asp:BoundField HeaderText="PrimaryKey" DataField="ManagerID" /> 
        <asp:BoundField HeaderText="Manager" DataField="FullName" />                                      
    ....                                       
</Columns>

Server side:

ManagerList_GV.DataSource = ??;
ManagerList_GV.DataBind();
ManagerList_GV.Columns[0].Visible = false;

This was deliberate so that the PK is not stored in ViewState for security reasons. You can then add a RowDeleting event and access primary key using

ManagerList_GV.Rows[e.RowIndex].Cells[0].Text;
Fermin
Interesting.. Would these commands full under the subroutine generated by visual studio, ManagerList_GV_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles ManagerList_GV.RowCommand? Or am I doing things in the wrong subroutine? It appears that e.RowIndex is null right now
Set CommandName of LinkButton to Delete and the event is called RowDeleting
Fermin
+4  A: 

I would create a button field but I think a template field should work as well:

<asp:ButtonField ButtonType="Link" Text="Delete" CommandName="Delete" />

Then you want to handle the RowDeleting Event of the Gridview:

Private Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles Gridview1.RowDeleting
    Dim ManagerIdToDelete As Int32 = CType(CType(sender, GridView).DataKeys(e.RowIndex).Value, Int32)
    'Now call your delete command with the id
End Sub
brendan
Is GridViewName_RowDeleting called automatically by asp.net on this event? Also, would having CommandName="Delete" not cause issues, since "Delete" is builtin to the system, as well? Or does having the CommandName in that way trigger this function?
Adding that CommandName = Delete is what fires ASP.Net's built in RowDeleting event.
brendan
Awesome, thanks :)