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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
GaialaAuthor 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.