2011-12-15 53 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"];