2014-05-01 47 views
3

我有一个GridView我用它来逐行传递信息到另一个页面。但是当我使列不可见时,它传递0作为值。这是我的GridView,与TaskId有问题。Invisible Gridview列获取0值

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

回答

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]); 
}