Fetching database information from gridviews that is not displayed in the gridview

I created a Gridview control that fetches some data from my database, but does not display all the information that I hope it collects. I prefer not to display the primary key for UI purposes, but it does require a key for basic grid operations.

I am currently trying to add a Delete option to a table, but unfortunately I have very little luck. Here is 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 created my stored procedure and business layer routine, but I'm not sure how to: a) pull which row was just clicked and then b) access the first key relative to that row, since I am not showing it (primary key - ManagerID).

Thanks in advance for your help :)

0


a source to share


2 answers


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

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

      



Then you want to handle the RowDeleting event in 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

      

+4


a source


Add a BoundColumn displaying the Primary Key as visible in design view, but after data binding, make the column with PK set to visible false.

eg.

<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 intentional so that the PC is not stored in ViewState for security reasons. Then you can add RowDeleting event and access the primary key using

ManagerList_GV.Rows[e.RowIndex].Cells[0].Text;

      

0


a source







All Articles