How do I get a value from by gridview in C#

I have a web page with a list of divisions in a gridview. I have a separate area on the page that allows the user to enter a division and when the user clicks on the Add to Grid button, it needs to check if the division already exists and display a message to the user.

From everything that I have read, I should be able to do the following but it is not working.  tb is returning null yet it works in my RowUpdating event.  Any help is greatly appreciated!

Here is the code that I am trying to get to work:
                Boolean unique = true;
                TextBox tb;
                // Validate the Division doesn't already exist
                foreach (GridViewRow row in gvDivisions.Rows)

                    tb = row.FindControl("tbEdit") as TextBox;
                    if (tbDivision.Text.ToUpper() == tb.Text.ToUpper())
                        unique = false;

Open in new window

This code in my gridView_RowUpdating event works:
                // UPdate the values in the datatable
                GridViewRow row = gvDivisions.Rows[e.RowIndex];
                // get currently selected value
                TextBox tb = gvDivisions.Rows[e.RowIndex].FindControl("tbEdit") as TextBox;
                dtDivision.Rows[row.DataItemIndex]["Division"] = tb.Text.ToUpper();
                DropDownList ddl = gvDivisions.Rows[e.RowIndex].FindControl("ddlStatus") as DropDownList;
                dtDivision.Rows[row.DataItemIndex]["ActiveStatusFlag"] = ddl.SelectedValue;

                // Reset the edit index
                gvDivisions.EditIndex = -1;

                // Bind the data
                gvDivisions.DataSource = dtDivision;

Open in new window

Here is my gridview markup:
            <asp:GridView ID="gvDivisions" runat="server" CellPadding="5" 
                OnPageIndexChanging="gvDivisions_PageIndexChanging" OnRowCancelingEdit="gvDivisions_RowCancelingEdit"
                OnRowEditing="gvDivisions_RowEditing" OnRowUpdating="gvDivisions_RowUpdating"
                AlternatingRowStyle-BackColor="#0099CC" AlternatingRowStyle-BorderColor="Black" 
                AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-ForeColor="White" 
                AlternatingRowStyle-HorizontalAlign="Left" AlternatingRowStyle-VerticalAlign="Middle" 
                BorderStyle="Solid" HeaderStyle-BackColor="Silver" BorderColor="Black" 
                HeaderStyle-BorderColor="Black" HeaderStyle-BorderStyle="Solid" 
                HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" 
                HorizontalAlign="Left" RowStyle-BorderColor="Black" RowStyle-BorderStyle="Solid" 
                RowStyle-HorizontalAlign="Left" RowStyle-VerticalAlign="Middle" 
                EmptyDataText="No Divisions Found" AllowPaging="True">
                <AlternatingRowStyle HorizontalAlign="Left" VerticalAlign="Middle" BackColor="#0099CC" BorderColor="Black" 
                    BorderStyle="Solid" ForeColor="White"></AlternatingRowStyle>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Silver" BorderColor="Black" 
                <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="Black" BorderStyle="Solid"></RowStyle>
                    <asp:BoundField DataField="DivisionID" HeaderText="ID" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Division">
                            <asp:TextBox ID="tbEdit" runat="server" ToolTip="Division" Text='<%# Bind("Division") %>'
                                CssClass="TextBoxFormat" MaxLength="30"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbDivisionRequiredFieldValidator2" runat="server" 
                                ErrorMessage="Division is required" BackColor="Red" Font-Bold="True" ForeColor="White"
                                ControlToValidate="tbEdit" Display="Dynamic" validationgroup="Info"
                            <asp:Label ID="lblDivision" runat="server" Text='<%# Bind("Division") %>'></asp:Label>                       
                    <asp:TemplateField HeaderText="Active">
                            <asp:DropDownList ID="ddlStatus" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            <asp:Label ID="lblActiveFlag" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>

Open in new window

Who is Participating?
guru_samiConnect With a Mentor Commented:
From what I understand, you have tbEdit in the EditItemTemplate, so it works in the RowUpdating but in your ItemTemplate there is no tbEdit but just lblDivision. So you might want to find a Label "lblDivision".
dyaroshAuthor Commented:
Such a simple solution!  I can't tell you how long I have been looking at this.  Thanks for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.