Find Text Inside Gridivew Cell






4.50/5 (2 votes)
Get the text present inside tablecell in both visible or hidden mode using VB.NET
Introduction
This article is used to demonstrate to the user how to get the text present on TableCell
inside a GridView
. The basic idea is bind your datavalue
inside a server side control and finally search for the control and get text from it. The process I have shown is also for a case when a particular column
is hidden.
Using the Code
In this sample application, there are two buttons, a button for finding text at column "Data Text" and a button that shows/hides data present under header "Name" and "Data Text".
The button for show/hide certain columns has the following click()
event:
//
// Button click event for show/hide columns.
//
Protected Sub btnShowHide_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnShowHide.Click
gvPopulate.Columns(0).Visible = Not gvPopulate.Columns(0).Visible
gvPopulate.Columns(2).Visible = Not gvPopulate.Columns(2).Visible
End Sub
Similarly, the button for finding the text present inside a cell have the following click()
event:
Dim myText As String = String.Empty
For Each row As GridViewRow In gvPopulate.Rows
Dim chkSelect As CheckBox = CType(row.FindControl("chkSelectName"), CheckBox)
If chkSelect.Checked Then
myText &= CType(row.FindControl("ltrDataText"), Literal).Text & " "
End If
Next
If myText <> String.Empty Then
ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType(), _
"message", "alert('" & myText & "');", True)
End If