How to set current page index from unique ID in dataset

J

Jim Mitchell

I can now find the right page index when I know the unique ID for a table
sorted by account name as show below.

But... Does anyone know a more efficient way other than looping through the
whole dataset?



daAccounts = New SqlClient.SqlDataAdapter("Select * from tblCompanys where
ID < 200 order by Company", mycn)

daAccounts.Fill(dsAccounts)

For i = 0 To dsAccounts.Tables(0).Rows.Count - 1

If dsAccounts.Tables(0).Rows(i).Item("ID") = KNOWN_UNIQUE_ID Then
TextBox1.Text = i

Next



dgAccounts.CurrentPageIndex = Int(TextBox1.Text / dgAccounts.PageSize)

dgAccounts.DataSource = dsAccounts

dgAccounts.DataKeyField = "ID"

dgAccounts.DataBind()
 
S

Stevie_mac

Just a thought, Why not store all yer IDs in a ViewState() (assuming theres not
thousands) or Session() variables and then retrieve a 'position' from that
eg...

Private m_iRowNumber as Integer

Private Sub dgAccounts_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
ViewState("ID" & e.Item.DataItem("ID")) = m_iRowNumber
m_iRowNumber += 1
End Sub

With that, you'd then be able to retrieve the position by simply doing...
Dim iRowNumber As Integer = ViewState("ID" & TextBox1.Text)
dgAccounts.CurrentPageIndex = iRowNumber / dgAccounts.PageSize
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top