如何从GridView隐藏主键但仍引用它

时间:2011-04-19 02:07:10

标签: c# asp.net gridview

我有一个从Linq查询构建的GridView,如下所示

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();

然后将GridView定义为:

<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" />

我想要做的是隐藏PermissionId字段,但显然仍需要删除按钮才能工作..

有人可以帮我解决这个问题吗?

我试过设置Visible =“false”,隐藏它,但是我的删除按钮停止工作..

为你的帮助干杯..

2 个答案:

答案 0 :(得分:4)

由于您正在设置DataKeyNames,因此您应该能够从正在删除的行的索引中检索权限ID,如下所示:

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

    DoDelete(permissionId);
}

无论列是否可见,这都应该有效。

答案 1 :(得分:1)

尝试

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