Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从GridView隐藏主键,但仍引用它_C#_Asp.net_Gridview - Fatal编程技术网

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

C# 如何从GridView隐藏主键,但仍引用它,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,它是根据Linq查询构建的,如下所示 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"]) ==

我有一个GridView,它是根据Linq查询构建的,如下所示

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字段,但它显然仍然需要存在,才能让Delete按钮正常工作

有人能帮我解决这个问题吗

我尝试设置Visible=“false”,这会隐藏它,但我的删除按钮停止工作

为你的帮助干杯

试试看

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

试试看


既然您正在设置
数据键名
,您应该能够从被删除行的索引中检索权限ID,如下所示:

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

    DoDelete(permissionId);
}

无论列是否可见,这都应该起作用。

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

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

    DoDelete(permissionId);
}

无论列是否可见,这都应该起作用。

实际上,从我看到的情况来看,它应该起作用,就像KaeL所说的,也许你应该发布删除按钮的代码。我记得我需要在GridView的onRowDatabound方法中处理commandArguments的传递,以便在onRowCommand方法中,delete按钮将id作为其参数。实际上,从我看到的情况来看,它应该可以工作,就像KaeL所说的,也许您应该发布delete按钮的代码。我记得我需要在GridView的onRowDatabound方法中处理commandArguments的传递,以便在onRowCommand方法中,delete按钮将id作为其参数