不可见的Gridview列得到0值

3

我有一个GridView,用于逐行传递信息到另一页。但是当我将一列设置为不可见时,它会将0作为值传递。这是我的GridView,在TaskId方面存在问题。

<asp:GridView ID="GridView1" runat="server" 
              Width="936px" AllowPaging="True" AutoGenerateColumns="False" 
              CellPadding="3" DataSourceID="SqlDataSource1" 
              OnRowCommand="GridView1_RowCommand" style="text-align: center" 
              BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" 
              BorderWidth="1px" CellSpacing="2" 
              OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
              AllowSorting="True">
    <Columns>
        <asp:BoundField DataField="TaskId" HeaderText="TaskId" Visible="false" 
                        ReadOnly="True" SortExpression="TaskId" />
        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
        <asp:BoundField DataField="Body" HeaderText="Body" 
                        SortExpression="Body" Visible="false" />
        <asp:BoundField DataField="Reward" HeaderText="Reward(Rs)" 
                        SortExpression="Reward" />
        <asp:BoundField DataField="TimeAllotted" HeaderText="Time(Min)" 
                        SortExpression="TimeAllotted" />
        <asp:BoundField DataField="PosterName" HeaderText="Uploader" 
                        SortExpression="PosterName" />
        <asp:ButtonField ButtonType="Button" CommandName="Select" 
                         Text="Perform Task" ControlStyle-ForeColor="White"  
                         ControlStyle-Font-Bold="true">
            <ControlStyle BackColor="#CC6600" Font-Bold="True" 
                          ForeColor="White"/>
        </asp:ButtonField>
    </Columns>
1个回答

3
你需要使用 GridViewDataKeyNames 属性。如果你想要访问它们的值,请在其中指定你不可见的列名。
<asp:GridView ID="GridView1" runat="server" DataKeyNames="TaskId" .../>

您可以这样访问该值:

foreach (GridViewRow row in GridView1.Rows)
{    
    int TASKID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接