0

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 つのセル値を取得したいと考えています。

値は次のとおりです。

  • 情報源
  • 表示ID
  • エピソードID

基本的にやりたい...

チェックされた行ごとに、列 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 に答える