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
GaialaAsked:
Who is Participating?
 
GaialaConnect With a Mentor Author Commented:
I use this because I have several dynamic created gridViews. And I need that GridView the user had clicked on the button of  the footer template
0
 
Pratima PharandeCommented:
get the count of the GridView  - GridView1.Rows.Count
0
 
Pratima PharandeCommented:
get the content of the first cell of the last row (count of the GridView)  --
GridView1.Rows(GridView1.Rows.Count - 1).Cells(0).Text
0
 
GaialaAuthor Commented:
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

0
 
Pratima PharandeCommented:
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
0
All Courses

From novice to tech pro — start learning today.