Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从隐藏列中获取值?_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何从隐藏列中获取值?

C# 如何从隐藏列中获取值?,c#,asp.net,gridview,C#,Asp.net,Gridview,下面是我的代码,它可以完美地处理可见列。但是,我需要能够获取某些不可见列的值: protected void btnSearchForSchedules_Click(object sender, EventArgs e) { var ds = new DataSet(); var schedulesTable = new DataTable(); foreach (GridViewRow gvr in gv

下面是我的代码,它可以完美地处理可见列。但是,我需要能够获取某些不可见列的值:

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]。文本


如果列不可见?

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

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

我看到了另一个问题,但我不知道如何在我的代码中使用它。这会有帮助吗?
string showId = (string) GridView1.DataKeys[2].Value.ToString();