Matt
Matt

Reputation: 4190

How do I hide my primary key from GridView but still reference it

I have a GridView that is built from a Linq query as follows

var GridViewLoad = from d in QVuser.QlikViewDashboards.AsEnumerable()
     join p in tempPermissions.AsEnumerable() on d.DashboardId equals Convert.ToInt32(p["DashboardId"])
     where  Convert.ToInt32(p["UserId"]) == GridViewUser
     select new 
     {
         DashboardId = d.DashboardId,
         PermissionId = Convert.ToInt32(p["PermissionId"]),
         DashboardName = d.DashboardName,
         Operational_Unit = p["Operational_Unit"].ToString(),
         Cost_Centre = p["Cost_Centre"].ToString(),
         Project = p["Project"].ToString(),
         Fund = p["Fund"].ToString()
     };

GridView1.DataSource = GridViewLoad; 
GridView1.DataKeyNames = new string[] {"PermissionId"};
GridView1.DataBind();

Then the GridView is defined as:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" 
    GridLines="Vertical" ShowHeaderWhenEmpty="True" OnRowDeleting="GridView1_RowDeleting"
        style="text-align: center" BackColor="White" BorderColor="#999999" 
    BorderStyle="None" BorderWidth="1px" Width="550px" 
   >
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
    <asp:TemplateField HeaderText="Delete" ShowHeader="False">
        <ItemTemplate>
            <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="false" 
                CommandArgument='<%# Eval("PermissionId") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="PermissionId" HeaderText="ID"/>
    <asp:BoundField DataField="DashboardName" HeaderText="Dashboard" ReadOnly="True" SortExpression="DashboardName" />
    <asp:BoundField DataField="Operational_Unit" HeaderText="Operational_Unit" ReadOnly="True" SortExpression="Operational_Unit" />
    <asp:BoundField DataField="Cost_Centre" HeaderText="Cost_Centre" ReadOnly="True" SortExpression="Cost_Centre" />
    <asp:BoundField DataField="Fund" HeaderText="Fund" ReadOnly="True" SortExpression="Fund" />
    <asp:BoundField DataField="Project" HeaderText="Project" ReadOnly="True" SortExpression="Project" />

</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" BorderStyle="Solid" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" BorderStyle="Solid" 
    BorderWidth="1px" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />

What I want to do is hide the PermissionId field from sight, but it obviously still needs to be there for the Delete button to work..

Can someone help me out with this?

I've tried setting Visible="false", which hides it, but then my delete button stops working..

Cheers for your help..

Upvotes: 1

Views: 1919

Answers (2)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107528

Since you're setting the DataKeyNames, you should be able to retrieve the permission ID from the index of the row being deleted, like so:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int permissionId = (int)GridView1.DataKeys[e.RowIndex].Value;

    DoDelete(permissionId);
}

This should work whether the column is visible or not.

Upvotes: 4

KaeL
KaeL

Reputation: 3659

Try

<asp:BoundField DataField="PermissionId" HeaderText="ID" Visible="False"/>

Upvotes: 1

Related Questions