我有一个网格视图,它存储带有分页的城市,每页有 10 个城市。
如何获取asp.net第二页上任何行的行索引?网格视图的名称是gdvCities。
我尝试了以下方法:
CheckBox chkcity = (CheckBox)sender;
GridViewRow gdvrow = (GridViewRow)chkcity.NamingContainer;
int rowindex = gdvrow.RowIndex;
对不起,迟到的帖子,但如果有人想知道,这是我的解决方案:
CheckBox chkcity = (CheckBox)sender;
GridViewRow gdvrow = (GridViewRow)chkcity.NamingContainer;
int rowindex = gdvrow.RowIndex;
var pagesize = this.GridView1.PageSize;
var pageindex = this.GridView1.PageIndex;
//basically, you want to take the pagesize multiplied by the page index, then subtract that from the rowIndex
paginatedRowIndex = rowindex - (pagesize * pageindex);
paginatedRowIndex 变量将是您在使用分页网格视图时要使用的行索引。
-萨汉
int rowindex = (gdvrow.RowIndex * gdvCities.PageIndex+1)
gdvrow.PageIndex 会给你页面的索引,默认情况下在第一页,页面索引是 0。所以加 1 得到准确的值
请尝试以下代码片段。
int rowindex = (gdvrow.RowIndex == 0 ? (gdvCities.PageIndex + 1) : (gdvrow.RowIndex * gdvCities.PageIndex + 1));
(GridView1.PageSize * GridView1.PageIndex) + e.row.rowIndex