asp.net如何得到GRIDVIEW中某行某列值的方法

來源:互聯網
上載者:User

根據某列的值改變其樣式最好的方法是在GridView的DataRowBound事件中想辦法。在GridView中的行綁定資料後將立即執行DataRowBound事件。DataRowBound事件使用GridViewRowEventargs類作為事件變數。通過事件變數你能夠利用GridViewRowEventArgs屬性操作已經綁定資料的行。 複製代碼 代碼如下:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
}

Row將返回TableRow類中的一個GridViewRow對象。
綁定的Row有幾種不同的類型。例如:DataRow, EmptyDataRow, Footer, Header, Pager 和 Separator。通過GridView的RowType屬性可以得到當前行的行類型。RowType是一組DataControlRow枚舉。
看下面的程式碼範例,檢測GridView列出的行是否為一個標準類型的行。複製代碼 代碼如下:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Do something!
}
}

可以使用Row的Cells屬性得到其Cells,它將返回一個TableCellCollection對象。然後通過TableCellCollection索引得到特定的Cells。TableCellcollection索引將返回一個TabelCell對象,對應於Row中的一個Cell:複製代碼 代碼如下:protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[0].Text;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.