0

我从 API 获取数据并将其放入数据表中:

DataTable showsTable = Transporter.GetDataTableFromAPI(callingURL);

gvShows.DataSource = showsTable;
gvShows.DataBind();

然后我在 dataBind 事件中向该表添加一个新列:

protected void gvShows_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvShows.Rows)
            {
                var tcCheckCell = new TableCell();
                var chkCheckBox = new CheckBox();
                tcCheckCell.Controls.Add(chkCheckBox);
                row.Cells.AddAt(0, tcCheckCell);
            }
        }

单击按钮时,我想根据检查的行获取 3 个单元格值。

这些值为:

  • 数据源
  • 节目编号
  • 剧集编号

我基本上想做...

对于每个选中的行,获取 dataSource、showId 和 episodeId 列的值,并将其放入 3 个不同的变量中。

我不确定这是否正确或如何完成:

foreach (GridViewRow row in gvShows.Rows)
            {
                CheckBox chk = row.Cells[0].FindControl("chkCheckBox") as CheckBox;
                if (chk != null && chk.Checked)
                {
                    dataSource = ...
                    showId = ...
                    episodeId = ...
                }
            }
4

1 回答 1

1
foreach (GridViewRow row in gvShows.Rows)
{
    CheckBox chk = row.Cells[0].FindControl("chkCheckBox") as CheckBox;
    if (chk != null && chk.Checked)
    {
        //Access the data written in cell
        dataSource = row.Cells[1].Text; 
        //if you are using DataKeys in GridView, which I particularly like to do
        //(and it's useful for data you do not dsiplay), you 
        //can access data like this:
        showId = gvShows.DataKeys[row.RowIndex]["showId"].ToString()
    }
}
于 2013-06-13T17:07:22.307 回答