e.item.cells and paged datagrid

Y

yer darn tootin

I have a paged datagrid that when built consists of several pages.
On each page there an ID number which is to be sent in a call another
page, like this:

Private Sub dgResults_ItemCommand(ByVal source As System.Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgResults.ItemCommand
If e.CommandName = "ViewID" Then
Response.Redirect("IDdetail.aspx?ID=" &
e.Item.Cells(2).Text.ToString)
End If
End Sub


Problem is that e.items.cells(2)is always taken from the corresponding
position on page 1. Eg Page one might have a list of ids 2,4,6,8 and
page 10 has ids 3,5,7,9. If I view page 10 and click 9 e.item.cells(2)
is equal to 8

Any suggestions on how to pass the correct ID?

Thanks,
Bob

*******************
The other code used

Page_load
Dim oDT as New DataTable
oDT = oDAL.GetAllID(fielda,fieldb, etc) -- call a sql sproc

If oDT.Rows.Count > 0 Then
dgResults.DataSource = oDT.DefaultView
dgResults.DataBind()
lblTotalRecs.Text = "Total Records: " & oDT.Rows.Count.ToString

Private Sub dgResults_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dgResults.PageIndexChanged
dgResults.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub

Private Sub BindGrid()
Dim oDV As New DataView
oDV = dgResults.DataSource
dgResults.DataSource = oDV
dgResults.DataBind()
oDV.Dispose()
End Sub
 
B

Brock Allen

You could pass the ID as the CommandArgument in your Button. It would make
the code much more clean and self-contained, rather than digging into the
Cells of the grid.
 
Y

yer darn tootin

Thanks guys.

Brock, have you got a snippet of code to show me how you'd do this?

The problem is that whatever's wrong, clicking a row's 'View' button on
page 2,3,4 etc always selects the correspondingly positioned ID off the
first page. My problem I can't figure out where the correct ID is being
stored, so I can't pass it to anything.

e.Item.Cells(2) is always an ID from the first page, and despite being
able to view ID's page 2,3,4 etc and click the corresponding view
button I can't get the correct ID passed, to anything, label or
otherwise.
 
Y

yer darn tootin

Workaround in place:
Instead of using the ItemCommand thingy I'm now using the ID field as a
HyperLink column in the datagrid,

ie
<asp:HyperLinkColumn Text="The ID " DataNavigateUrlField="ID"
DataNavigateUrlFormatString="IDdetail.aspx?id={0}"
DataTextField="ID" </asp:HyperLinkColumn>

Much simpler. Not sure what my colleague who wrote in the ItemCommand
was thinking, but to be fair I think on a non-paged datagrid it'd work
fine, that's how the page started out.
 
B

Brock Allen

Brock, have you got a snippet of code to show me how you'd do this?

In the ASPX ItemTemplate:

<asp:Button runat=server .... CommandName="ViewID" CommandArgument='<%# DataBinder.Eval(Container.DataItem,
"ID")%>'

Then in your code, ItemCommand, the Button's CommandArgument will be the
ID for that row form the DB.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top