2011-04-19 57 views
1

我有一個從LINQ查詢建成一個GridView如下如何隱藏我的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字段,但它顯然仍然需要在那裏爲刪除按鈕才能工作..

有人可以幫我解決這個問題嗎?

我試過設置可見= 「假」,隱藏它,但後來我的刪除按鈕停止工作..

乾杯您的幫助..

回答

4

由於您正在設置DataKeyNames,因此您應該能夠從被刪除行的索引中檢索權限ID,如下所示:

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

    DoDelete(permissionId); 
} 

無論列是否可見,這都應該起作用。

+0

工作。謝謝Cory – Matt 2011-04-19 02:28:27

1

嘗試

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

是的,我試過,但那導致刪除按鈕停止工作.. – Matt 2011-04-19 02:14:01

+0

你可以發佈您的代碼爲您的刪除按鈕.. – KaeL 2011-04-19 02:18:19

+1

其實從我看到的,它應該工作,像KaeL說,也許你應該發佈您的刪除按鈕的代碼。我記得我需要在GridView的onRowDatabound方法中處理commandArguments的傳遞,以便在onRowCommand方法中,刪除按鈕的ID將作爲它的參數 – 2011-04-19 02:30:13

相關問題