这是我的代码,它与可见列完美配合。但是,我需要能够获得不可见的某些列的值:
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);
}
}
}
任何人都可以向我解释我是如何做到的:
string showId = gvr.Cells [2] .Text;
如果列不可见?
答案 0 :(得分:0)
如果要获取隐藏的列值,请在aspx页面中为该GridView指定DataKeyNames
属性。
<asp:GridView ID="GridView1" runat="server" DataKeyNames="column1" ...>
然后您可以在后面的代码中检索该列值,如下所示。
string showId = (string) GridView1.DataKeys[2].Value.ToString();