Nandini
Nandini

Reputation: 393

Issues with Asp.net Gridview Paging

I have used the Gridview_PageIndexChanging event in asp.net.i have used the code like this:

gridFileDetails.PageIndex = e.NewPageIndex

During the run time when i clicked the next page,it generates an error:

An exception of type 'System.InvalidCastException' occurred in FFK.DLL but was not handled in user code

Additional information: Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'.

in the RowCommand event,

I have used the following RowCommand event:

Protected Sub gridFileDetails_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridFileDetails.RowCommand

    Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
    Dim rowkey As DataKey = Me.gridFileDetails.DataKeys(row.DataItemIndex)
    Dim key As String = rowkey(0).ToString()
    If e.CommandName = "FileStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("FFKFile.aspx?FileId=" + key)
    ElseIf e.CommandName = "TradexStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("TradeX.aspx?FileId=" + key)
    ElseIf e.CommandName = "BondStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("BondMaster.aspx?FileId=" + key)
    ElseIf e.CommandName = "FDStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("FrmFileDocument.aspx?FileId=" + key)
    ElseIf e.CommandName = "InvoiceStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("InvoiceMaster.aspx?FileId=" + key)
    ElseIf e.CommandName = "PDStatus" And e.CommandArgument <= 0 Then
        Response.Redirect("PackagingDetails.aspx?FileId=" + key)
    End If
End Sub

can you resolve this problem? i am getting the error in the first line in RowCommand event ,ie.,

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

In the PageIndexChanging event i have written as follows:

Protected Sub gridFileDetails_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gridFileDetails.PageIndexChanging gridFileDetails.PageIndex = e.NewPageIndex End Sub

Upvotes: 0

Views: 4841

Answers (3)

Canavar
Canavar

Reputation: 48088

This might help :

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gridFileDetails.Rows(index)

Upvotes: 1

Jay S
Jay S

Reputation: 7994

Referencing the MSDN for the System.Web.UI.WebControls.GridViewCommandEventArgs class, the example there for extracting the gridviewrow is this:

  ' Convert the row index stored in the CommandArgument
  ' property to an Integer.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

  ' Retrieve the row that contains the button clicked 
  ' by the user from the Rows collection.
  Dim row As GridViewRow = ContactsGridView.Rows(index)

See if that helps you get your row without the cast error.

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351446

Instead of this:

Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)

Use the index from the EventArgs to grab the row programmatically like this:

Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = gridFileDetails.Rows(index)

Upvotes: 1

Related Questions