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].Text;

如果该列不可见?

4

1 回答 1

0

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

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

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

string showId = (string) GridView1.DataKeys[2].Value.ToString();
于 2013-06-18T22:23:14.803 回答