我如何获得基于同一行的复选框选择的网格视图上特定单元格的值

本文关键字:视图 单元格 网格 复选框 何获得 于同一 选择 | 更新日期: 2023-09-27 18:18:10

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

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个单元格值。

取值为:

  • 数据源
  • showId
  • episodeId

我基本上想做…

对于每个选中的行,获取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 = ...
                }
            }

我如何获得基于同一行的复选框选择的网格视图上特定单元格的值

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()
    }
}