开发者

ASP.NET Gridview: Get the PageIndex from the selected Row

I need the functionality to highlight the edited row in a gridview, so on RowDataBound of the gridv开发者_JAVA百科iew i check which one's the one that has been edited and change the css style. There is one small problem, the change could make the row appear on a different page. How do i get the PageIndex from the row to jump to that page ?


untested code in vb.net


*assuming there are studentid from 1 to 100 in the database
*assuming pagesize of gridview is 10


dim studentid_of_edited_row as integer = 11


dim da as new dataadapter(strquery,conn)
dim dt as new datatable
da.fill(dt)


dim desired_pageindex as integer = 0


for i as integer = 0 to dt.rows.count - 1
  if dt.rows(i)("studentid") = studentid_of_edited_row then
    desired_pageindex = i / gridview1.pagesize
    exit for
  end if
next


gridview1.pageindex = desired_pageindex
gridview1.datasource = dt
gridview1.databind


Look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindex.aspx

The property PageIndex lets you get or set the index of the currently displayed page.


I would add an extra column that contains RowNumber. If you are using MSSQL it is easy as

SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 [DESC]) as RowNumber FROM table

and find the changed row int DataTable.Rows using DataTable.Rows.Select that matches all column values(if you have an unique column sure you can use that, if you don't you have no luck! you could get more than one row soo choose one! if uniqueness is not soo important that doesn't make any sense) and get RowNumber value which is also the column you added to the table by using ROW_NUMBER() TSQL function. And the rest is math;

int PageNumber = PageSize < RowNumber ? ((RowNumber - 1) / PageSize) + 1 : 1;

EDIT: 1- I assume you querying database for each roundtrip the page 2- ROW_NUMBER() should work Oracle and MySql i beleive or has equal operator that i know of...


When you are editing the selected row, you are obviously going to need some unique identifier to save your change back to the data store. Save that identifier to a local variable. Then when you are looking at each row in the OnRowDataBound event, compare its unique identifier to the currently saved value. If it's the same, you've found your row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜