2013-06-18 82 views
0

下面是我有的代码,它与可见列完美配合。不过,我需要能够得到某些列是看不到的价值:如何从隐藏列中获取值?

protected void btnSearchForSchedules_Click(object sender, EventArgs e) 
     { 
      var ds = new DataSet(); 
      var schedulesTable = new DataTable(); 

      foreach (GridViewRow gvr in gvShows.Rows) 
      { 
       if (gvr.RowType == DataControlRowType.DataRow) 
       { 
        if (((CheckBox) gvr.FindControl("cbSelect")).Checked) 
        { 
         string baseURL = WebConfigurationManager.AppSettings["SchedulesAPI"]; 

         string dataSource = gvr.Cells[1].Text; 
         string showId = gvr.Cells[2].Text; //this column is hidden so I'm getting a NULL here 
         string episodeId = gvr.Cells[4].Text; 

         string callingURL = baseURL + "?datasource=" + dataSource + "&showid=" + showId; 

         if (episodeId != " ") 
         { 
          callingURL = callingURL + "&episodeId=" + episodeId; 
         } 

         schedulesTable = Transporter.GetDataTableFromAPI(callingURL); 

         ds.Tables.Add(schedulesTable); 
        } 
       } 
      } 

任何人都可以向我解释,我怎么可以这样做:

串showId = gvr.Cells [2] 。文本;

如果该列不可见?

+0

http://stackoverflow.com/q/4127662/2387010 – Chris

+0

的可能的复制,我看到其他的问题,但我不知道如何让它在我的代码工作。 –

+0

这个http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/e7bf9a9c-f5c6-4b03-8481-652dc1c9d7b0/会有帮助吗? – Chris

回答

0

当您想要获取隐藏列值时,您可以在aspx页面中为该GridView指定DataKeyNames属性。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="column1" ...> 

然后,您可以在后面的代码中检索该列值,如下所示。

string showId = (string) GridView1.DataKeys[2].Value.ToString();