ASP.NET系列之點滴積累(一):GridView中擷取選定行中資料

來源:互聯網
上載者:User

上次做一個網站的時候,遇到一個問題: 在綁定了資料的GridView 中,我需要把選定的行中資料讀取出來儲存到一個結構體中以便將這些資料傳遞到下一個頁面,問了一些朋友,找的些資料,最終沒有結果,一個朋友說可以把資料行的ID傳遞到下一個頁面時再進行綁定,當時一想,這辦法可以,但當我去做的時候才知道當我得到上面頁面傳遞的ID後還需要進行綁定太麻煩了,我相信通過結構體的方式傳遞到下頁更方便,所以我一直在找方法,今天早上才發現,擷取某行資料並不難:

首先添加一模版列用來選定或者擷取當前選中行的行號,然後通過GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;這樣的方式擷取想要的資料單元。只需把沒個單中繼資料儲存為一個結構體即可。

注意的是,我在設定模版列時把GridView的CommandName屬性設定為select(這是預設值),這樣才能擷取行號。

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
 ...{     
        ProviderDetail pdetail = new ProviderDetail();
        pdetail.sGameName = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
        pdetail.sAreaName = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
        pdetail.sServerName = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
        pdetail.nMaxNum = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
        pdetail.sPrice = GridView1.Rows[e.NewSelectedIndex].Cells[4].Text;
        pdetail.sMinDeal = GridView1.Rows[e.NewSelectedIndex].Cells[5].Text;            

        Session["PD"] = pdetail;
        Response.Redirect("~/ProviderDetails.aspx");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.