Click here to Skip to main content
13,256,841 members (52,051 online)
Click here to Skip to main content
Add your own
alternative version


68 bookmarked
Posted 20 Oct 2005

Accessing the different controls inside a GridView control

, 20 Oct 2005
Rate this:
Please Sign up or sign in to vote.
How to access the different controls inside a GridView control.


I receive many emails asking how we can access a particular control which resides inside a GridView control. In this article I will show you how you can access different controls inside a GridView control. We will see how we can access a TextBox control, a DropDownList control and a ListBox control. If you are working with ASP.NET 1.X then you might want to check out my article Accessing Different Controls Inside a DataGrid.

Adding controls to the GridView control

You can add several controls to the GridView control by simply using the <ItemTemplate> option.

Populating ListBox and DropDownList

The next task is to populate the ListBox and the DropDownList control. Let's make a simple server side method that will populate both the ListBox and the DropDownList.

C# Code

// This method populates the DropDownList and the ListBox control
public DataSet PopulateControls()
    SqlConnection myConnection = new SqlConnection(GetConnectionString());
    SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson", 
    DataSet ds = new DataSet();
    ad.Fill(ds, "tblPerson");
    return ds;


' This method populates the DropDownList and the ListBox control
Public Function PopulateControls() As DataSet
    Dim myConnection As SqlConnection = New SqlConnection(GetConnectionString())
    Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT " & _ 
                               "[Name] FROM tblPerson",myConnection)
    Dim ds As DataSet = New DataSet()
    ad.Fill(ds, "tblPerson")
    Return ds
End Function

Now we need to bind this method in the HTML view. Check out the code below for the DropDownList, you can repeat the same procedure for the ListBox control.

  <asp:DropDownList ID="DropDownList1" DataTextField="Name" 

    DataValueField = "Name" DataSource= '<%# PopulateControls() %>' runat="server">

Now your DropDownList and the ListBox control are populated with some data. Now let's see how we can access different controls inside the GridView.

Accessing different controls within the GridView control

On the Button click event, we will try to print out the values that are either entered (TextBox) or selected (DropDownList and ListBox). Let's see how this can be done.

C# Code

protected void Button1_Click(object sender, EventArgs e)
    // Iterates through the rows of the GridView control
    foreach (GridViewRow row in GridView1.Rows)
        // Selects the text from the TextBox
        // which is inside the GridView control
        string textBoxText = _
        // Selects the text from the DropDownList
        // which is inside the GridView control
        string dropDownListText = ((DropDownList)
        // Selects items from the ListBox
        // which is inside the GridView control
        ListBox myListBox = (ListBox)row.FindControl("ListBox1");

        foreach(ListItem selectedItem in myListBox.Items)
            // Checks if the item in the ListBox is selected or not
            if (selectedItem.Selected)
                // Print the value of the item if its selected


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Iterates through the rows of the GridView control
        For Each row As GridViewRow In GridView1.Rows
            ' Selects the text from the TextBox
            ' which is inside the GridView control
            Dim textBoxText As String = _
            ' Selects the text from the DropDownList
            ' which is inside the GridView control
            Dim dropDownListText As String = _
              CType(row.FindControl("DropDownList1"), _
            ' Selects items from the ListBox
            ' which is inside the GridView control 
            Dim myListBox As ListBox = _
            For Each selectedItem As ListItem In myListBox.Items
                ' Checks if the item in the ListBox is selected or not 
                If selectedItem.Selected Then
                    ' Print the value of the item if its selected
                End If
    End Sub

All we are doing in the code above is iterating through all the rows of the GridView control using the GridViewRow object. Next we find the control using the FindControl method and prints out the control's value.

I hope you liked the article, happy coding!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
I am the founder of knowledge base website, HighOnCoding, GridViewGuy, and

HighOnCoding is a website which will get you high legally with useful information. There are tons of articles, videos and podcasts hosted on HighOnCoding.

My Blog:


Buy my iPhone app ABC Pop

You may also be interested in...


Comments and Discussions

Questionsearch the selected row in edit event rather than entire gridview Pin
htm1123-Dec-15 5:59
memberhtm1123-Dec-15 5:59 
GeneralNice Code Pin
Member 1121106916-Jun-15 21:38
memberMember 1121106916-Jun-15 21:38 
GeneralMy vote of 1 Pin
JB030124-Apr-14 17:15
professionalJB030124-Apr-14 17:15 
GeneralVery nice Article Pin
munirshaikh14-Oct-13 2:26
membermunirshaikh14-Oct-13 2:26 
GeneralVery nice Article--- thank you Pin
balaramanN22-May-14 1:44
memberbalaramanN22-May-14 1:44 
QuestionRegarding web solutions Pin
ftgd19-Jun-12 2:41
memberftgd19-Jun-12 2:41 
GeneralVery Easy to Learn Pin
ayyappanit18-Jan-11 23:52
memberayyappanit18-Jan-11 23:52 
GeneralVery Easy to Learn Pin
ayyappanit18-Jan-11 23:51
memberayyappanit18-Jan-11 23:51 
QuestionGridview FindControl Pin
syam@tnt24-Oct-10 19:21
membersyam@tnt24-Oct-10 19:21 
QuestionGridview FindControl Pin
syam@tnt24-Oct-10 19:19
membersyam@tnt24-Oct-10 19:19 

can you pl confirm how to take below textbox values
<asp:GridView ID="gvProducts" runat="server" AllowPaging="True" <br />
                                    AutoGenerateColumns="False" CssClass="igoogle" <br />
                                    PageSize="5" ShowFooter="True" <br />
                                    Width="100%"><br />
                                    <AlternatingRowStyle BackColor="#e5e4cf" /><br />
                                    <EmptyDataTemplate><br />
                                        <asp:PlaceHolder ID="ProductEmpty" runat="server"><br />
                                            <table width="100%"><br />
                                                <tr><br />
                                                    <td align="center"><br />
                                                        Product Code<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        Unit Qty<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        Price<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        Gross Amount<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        Discount %<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        Net Amount<br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                    </td><br />
                                                </tr><br />
                                                <tr style="background-color: White;"><br />
                                                    <td><asp:DropDownList ID="cmbProduct" runat="server"  DataSource="<%#GetProductList()%>"<br />
                                                            DataTextField="productname" DataValueField="productcode" ><br />
                                                            </asp:DropDownList><br />
<br />
                                                        <br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        <asp:TextBox ID="txtQty" runat="server" class="NormalTextBox" MaxLength="10" <br />
                                                            onblur="this.className='NormalTextBox';return CheckQtyVal();" <br />
                                                            onfocus="this.className='NormalTextBoxFocus'"   onchange="return CheckQtyVal();" > </asp:TextBox><br />
                                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="**"  ControlToValidate="txtQty" ValidationGroup="vgins"></asp:RequiredFieldValidator><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                     <br />
                                                        <asp:TextBox ID="txtPrice" runat="server" class="NormalTextBox" MaxLength="10" <br />
                                                            onblur="this.className='NormalTextBox';return CheckPriceVal();" <br />
                                                            onfocus="this.className='NormalTextBoxFocus'"   onchange="return CheckPriceVal();" > </asp:TextBox><br />
                                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="**"  ControlToValidate="txtPrice" ValidationGroup="vgins"></asp:RequiredFieldValidator><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        <asp:TextBox ID="txtGrossamt" runat="server" ReadOnly="true" class="NormalTextBox" <br />
                                                            MaxLength="10" onblur="this.className='NormalTextBox';" <br />
                                                            onfocus="this.className='NormalTextBoxFocus'"></asp:TextBox><br />
                                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="**"  ControlToValidate="txtGrossamt" ValidationGroup="vgins"></asp:RequiredFieldValidator><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        <asp:TextBox ID="txtdiscount" runat="server" class="NormalTextBox" <br />
                                                            MaxLength="10" onblur="this.className='NormalTextBox';return ChangeValue();" onchange="return ChangeValue();"  <br />
                                                             onfocus="this.className='NormalTextBoxFocus'" ></asp:TextBox><br />
                                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="**"  ControlToValidate="txtdiscount" ValidationGroup="vgins"></asp:RequiredFieldValidator><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        <asp:TextBox ID="txtNetamt" runat="server" class="NormalTextBox" ReadOnly="true" MaxLength="10" <br />
                                                            onblur="this.className='NormalTextBox';" <br />
                                                            onfocus="this.className='NormalTextBoxFocus'"></asp:TextBox><br />
                                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="**"  ControlToValidate="txtNetamt" ValidationGroup="vgins"></asp:RequiredFieldValidator><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                    </td><br />
                                                    <td align="center"><br />
                                                        <asp:ImageButton ID="imgNewProductInsert" runat="server" <br />
                                                            CausesValidation="true" ImageUrl="~/images/add1-24.png" <br />
                                                            OnClick="imgNewProductInsert_Click" ValidationGroup="vgins" /><br />
                                                         <asp:ImageButton ID="imgNewProductClear" runat="server" <br />
                                                            ImageUrl="~/images/cancel24.png" /><br />
                                                    </td><br />
                                                </tr><br />
                                            </table><br />
                                        </asp:PlaceHolder> <br />
                                    </EmptyDataTemplate><br />
                                    <PagerStyle CssClass="footer" /><br />
                                </asp:GridView><br />
<br />
 protected void imgNewProductInsert_Click(object sender, ImageClickEventArgs e)<br />
    {<br />
                TextBox mQty = (TextBox)gvProducts.Controls[0].Controls[0].FindControl("txtPrice");<br />
                TextBox mPrice = (TextBox)gvProducts.Controls[0].Controls[0].FindControl("txtPrice");<br />
                TextBox mdiscount = (TextBox)gvProducts.Controls[0].Controls[0].FindControl("txtdiscount");<br />
                <br />

QuestionGridview FindControl Pin
syam@tnt24-Oct-10 19:18
membersyam@tnt24-Oct-10 19:18 
GeneralVery good Pin
Member 475746017-Aug-09 9:37
memberMember 475746017-Aug-09 9:37 
GeneralMy vote of 1 Pin
Joe Gakenheimer )16-Dec-08 3:12
memberJoe Gakenheimer )16-Dec-08 3:12 
QuestionHow to access last row of gridview... Pin
frifun29-Aug-08 4:13
memberfrifun29-Aug-08 4:13 
Generaldidnt get the value Pin
nitendra7-Mar-08 2:28
membernitendra7-Mar-08 2:28 
GeneralGet Checkbox values of Gridviewcontrol in button click Pin
senjith14-Oct-07 4:43
membersenjith14-Oct-07 4:43 
QuestionHow to get the new value Pin
beaglepuppy23-Feb-07 12:34
memberbeaglepuppy23-Feb-07 12:34 
GeneralMultiple TextBoxes Pin
superstringman5-Jan-07 3:59
membersuperstringman5-Jan-07 3:59 
GeneralAccess a control value in the Editing Row. Pin
ra ra ra ra21-Dec-06 8:26
memberra ra ra ra21-Dec-06 8:26 
GeneralDoes not work for my gridview. Pin
Break4022-Sep-06 11:02
memberBreak4022-Sep-06 11:02 
GeneralRe: Does not work for my gridview. Pin
azamsharp22-Sep-06 11:10
memberazamsharp22-Sep-06 11:10 
GeneralRe: Does not work for my gridview. [modified] Pin
Break4022-Sep-06 11:23
memberBreak4022-Sep-06 11:23 
Questiongridview header Pin
o5ama14-Aug-06 22:31
membero5ama14-Aug-06 22:31 
Questionhow to access dataItem Pin
xz2317-Jan-06 18:13
memberxz2317-Jan-06 18:13 
AnswerRe: how to access dataItem Pin
azamsharp17-Jan-06 18:18
memberazamsharp17-Jan-06 18:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by azamsharp
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid