views:

1767

answers:

4

I need the functionality to highlight the edited row in a gridview, so on RowDataBound of the gridview 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 ?

+2  A: 
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
mangokun
I thought about this solution too, but it's not that optimal and elegant at all. It's alot of overhead work to iterate through the rows just for that.
Fiur
@Fiur: I'm not sure what the "overhead" is. You're already re-binding your datasource, so that means you *have* it. If you have it, why not loop thru it to find the right row? By your own admissions, the row could show up anywhere, and there's no way of finding it without searching for it. As a note, his solution only added that loop to code you're already doing, plus the desired page variable / setting. That's +8 lines, and 1 loop. Not exactly "a lot of overhead"
JustLoren
+1  A: 

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.

RS
A: 

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.

cnobles
That's exactly what i am doing and i can find that row back very easil, my problem and the question i'm asking is how do i get the page index from the row?
Fiur
+1  A: 

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...

Emrah GOZCU