2011-12-15 52 views
2

我的窗体上有一些gridvew字段。我在gridview中添加了两个字段,因为我想在后面的代码中使用它们中的数据,并在代码后面读取它们,但事情是我不希望这些列在gridview中可见,所以我试图设置他们的可见属性'假',但没有奏效,我没有访问他们的数据。如何实现?Gridview的Boundfield问题

 <asp:BoundField DataField="Service_Id" HeaderText="Service_Id" SortExpression="Service_Id" HeaderStyle-BackColor="Gray" 
      Visible="true"> 
      <HeaderStyle BackColor="Gray"></HeaderStyle> 
     </asp:BoundField> 
     <asp:BoundField DataField="UserId" HeaderText="UserID" SortExpression="UserId" HeaderStyle-BackColor="Gray" 
      Visible="true"> 
      <HeaderStyle BackColor="Gray"></HeaderStyle> 
     </asp:BoundField> 

这是我的页面的后台代码:

Button Button1 = (Button)sender; 
     GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent; 
     HiddenFieldServiceID.Value = grdRow.Cells[0].Text; 
     HiddenFieldUserID.Value = grdRow.Cells[1].Text; 

回答

5

你不应该使用BoundField这一点。改为使用DataKeyNames属性。 的值,然后可以使用DataKeys[rowIndex]

ASPX被撷取:

DataKeyNames="Service_Id, UserId"; 

代码:

var Service_Id = (int)gv.DataKeys[rowIndex]["Service_Id"]; 
var UserId = (int)gv.DataKeys[rowIndex]["UserId"];