How to get the value of the selected row in a gridview?
To obtain the value of the selected row in GridView, you can achieve it by following these steps:
- Create an event handler to handle the event of selecting a row. You can use the SelectedIndexChanged event of the GridView.
- In the event handler, use the SelectedRow property to access the selected row.
- Next, you can use the Cells property to retrieve the cells in the selected row.
- Finally, you can use the Value property of the cell to retrieve its value.
Here is a sample code demonstrating how to retrieve the values of selected rows in a GridView.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
// 获取选中行
GridViewRow selectedRow = GridView1.SelectedRow;
// 获取选中行中的单元格
TableCellCollection cells = selectedRow.Cells;
// 获取单元格的值
string value1 = cells[0].Text; // 第一列的值
string value2 = cells[1].Text; // 第二列的值
// ...
}
Please note that if the GridView is enabled with pagination, only the values of the selected rows on the current page can be retrieved. If you need to obtain the values of all selected rows, consider iterating through all the rows in the GridView and checking if each row is selected.