Link to home
Start Free TrialLog in
Avatar of Gaiala
GaialaFlag for Germany

asked on

get last GridView row

Hello,

to insert new data records into a GridView I use the Footertemplate. Each GridView row has an id, text, etc from a xmlfile tag.  The problem is that I have to get the Id from the last GridView row so I have an insertion point in the xml file.
so in the button event:
- I nee to get the count of the GridView
- get the content of the first cell of the last row (count of the GridView)

thank you in advice
Avatar of Pratima
Pratima
Flag of India image

get the count of the GridView  - GridView1.Rows.Count
get the content of the first cell of the last row (count of the GridView)  --
GridView1.Rows(GridView1.Rows.Count - 1).Cells(0).Text
Avatar of Gaiala

ASKER

ok I have tried to test your example. The code is set in the button event of a footer button.
But I get an exception :(
Unable to cast object of type 'System.Web.UI.WebControls.GridViewRow' to type 'System.Web.UI.WebControls.GridView'.
 Public Sub btnFo_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
        Try
            Dim ddl1 As Button = CType(sender, Button)
            Dim row As GridViewRow = CType(ddl1.NamingContainer, GridViewRow)
            ' here I get an exception
           Dim gv As GridView = CType(ddl1.NamingContainer, GridView)
            Dim strText As String = gv.Rows(gv.Rows.Count - 1).Cells(0).Text()
 
            Dim txtInfo As TextBox
            Dim txtInput As TextBox
            Dim strInfo As String
            Dim strInput As String
            txtInfo = CType(row.FindControl("foTxt01"), TextBox)
            txtInput = CType(row.FindControl("foTxt02"), TextBox)
            strInfo = txtInfo.Text
            strInput = txtInput.Text
 
        Catch ex As Exception
 
        End Try
 
    End Sub
End Class
Public Class FooterTemplateText
    Implements ITemplate
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Try
            Dim txtFo As New TextBox
            txtFo.ID = "foTxt01"
            txtFo.TextMode = TextBoxMode.MultiLine
            txtFo.Visible = True
            container.Controls.Add(txtFo)
        Catch ex As Exception
 
        End Try
 
    End Sub
 
End Class

Open in new window

Dim gv As GridView = CType(ddl1.NamingContainer, GridView)

why you used this ? did you want to create new grid view ?
gv.Rows(gv.Rows.Count - 1).Cells(0).Text()
This will give you result if gridview is already present & binded
ASKER CERTIFIED SOLUTION
Avatar of Gaiala
Gaiala
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial