Find control value on dynamically created gridview

RobertNZana used Ask the Experts™
I have a gridview which I dynamically bind in the page_load event. On the gridview1_RowDataBound event I want to check the value of the 2nd column (called "BookTitle"). How do I access this controls value since it's dynamically created? Code samples preferred. Thanks.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

might not have fully understood your question, let me know if i have but try the following.

put this into your row databound event

//Check row state of gridview whether it is data row or not
if (e.Row.RowType == DataControlRowType.DataRow)
    TextBox MyTextBox= (TextBox )(e.Row.FindControl("MyTextBox"));
    MyTextBox.Text = "MyValue"; // or what ever you need to do with it


I know how to do that already. My problem is that the gridview is DYNAMICALLY created so I don't have a textbox, label, etc... with a predefined control name. So I cannot use FindControl by the name.

not how do that sure, sorry i misread your question. Would it be feasable in your app to hide the gridview in a panel instead of creating it dynamically.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Have u created the gridview dynamically with the templatefield or the boundfield?

If it is templatefield, u can find the control as said by Ross-C.

If it is bound field u can find using the e.cells

My friend, to add a handler in a dynamically created control, use the addHandler method.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim dt As New Data.DataTable
        Dim r As Data.DataRow = dt.NewRow
        r.Item("test1") = "a"

        Dim grd2 As New GridView
        grd2.ID = "grdMyDynamicGrid"
        grd2.DataSource = dt
        AddHandler grd2.RowDataBound, AddressOf GridView1_RowDataBound
    End Sub

    Protected Sub Grd2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Response.Write("Adding line in " + CType(sender, Control).ID + Environment.NewLine)
        'Do what you want here
     End Sub

Open in new window


So in the RowDataBound for this case how would I search for the value in the 2nd column? Please post a code sample.
If you want a reference to your gridView, it is provided by the Sender object.
If you just want to check the value of the 2nd column, this is provided by e.row

So, in the RowDataBound do this:
Protected Sub Grd2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim myGridView as GridView = ctype(sender, GridView)
        Dim SecondColumnText as string = e.Row.Cells(1).text
End Sub

Don't get confused with the fact that your gridview is dynamically created, the RowDataBound event works exactly as it would with a gridview created in design time.

Let me know if you need any more help with this.
Just use


in row databopund if u have used bound field

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial