ASP.NET Gridview: get PageIndex value from selected row
I need the functionality to highlight the edited row in the gridview, so in the RowDataBound of the gridview I check which one was edited and change the css style. There is one small problem, the change may cause the row to appear on a different page. How do I get the PageIndex value from a string to navigate to that page?
source to share
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
source to share
Take a look here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pageindex.aspx
The PageIndex property allows you to get or set the index of the currently displayed page.
source to share
I would add an extra column containing RowNumber. If you are using MSSQL this is easy as
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 [DESC]) as RowNumber FROM table
and find the modified row int DataTable.Rows using DataTable.Rows.Select that matches all the column values (if you have a unique column you can use it, if you're out of luck you can get more than one row soo selects one, if uniqueness is not important to soo, which makes no sense) and get the RowNumber value, which is also the column added to the table, using TSQL ROW_NUMBER () function. The rest is mathematics;
int PageNumber = PageSize < RowNumber ? ((RowNumber - 1) / PageSize) + 1 : 1;
EDIT: 1- I am assuming you are querying the database for each round on page 2- ROW_NUMBER () should work Oracle and MySql I beleive or have an equal operator that I know ...
source to share
When you edit the selected row, you obviously need some kind of unique identifier to save your change back to the data store. Store this identifier as a local variable. Then, when you look at each row in the OnRowDataBound event, compare its unique ID with the currently stored value. If it's the same, you've found your string.
source to share