2014-05-01 40 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]); 
}