Gridview FindControl in ASP.Net


.aspx
<asp:GridView ID="gridfindcontrol" runat="server" AutoGenerateColumns="false" ShowFooter="true"
            OnRowDataBound="gridfindcontrol_RowDataBound" OnRowCommand="gridfindcontrol_OnRowCommand">
            <Columns>
                <asp:TemplateField HeaderText="S.No.">
                    <ItemTemplate>
                        <asp:Label ID="lblsno" runat="server" Text=' <%#Container.DataItemIndex+1 %>'></asp:Label></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="TexBox">
                    <ItemTemplate>
                        <asp:TextBox ID="txt" runat="server" AutoPostBack="true" OnTextChanged="txt_TextChanged">
                        </asp:TextBox>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtfoot" runat="server" AutoPostBack="true" OnTextChanged="txtfoot_TextChanged">
                        </asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Label">
                    <ItemTemplate>
                        <asp:Label ID="lbl" runat="server" Text='<%#Bind("DATA") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Label ID="lblfoot" runat="server" Text="Label"></asp:Label></FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Link">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnk" runat="server" CommandName="OK" CommandArgument='<%#Bind("DATA") %>'
                            Text='<%#Bind("DATA") %>'></asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="lnkfoot" runat="server" CommandName="OKFOOT" CommandArgument="Footer"
                            Text="Click"></asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

.aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }
        private void BindGrid()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("DATA");

            for (int PK = 0; PK < 10; PK++)
            {
                DataRow dr = dt.NewRow();
                dr["DATA"] = "DATA" + PK;

                dt.Rows.Add(dr);
            }

            gridfindcontrol.DataSource = dt;
            gridfindcontrol.DataBind();
        }
        protected void txt_TextChanged(object sender, EventArgs e)
        {
            //GridViewRow row = ((GridViewRow)((TextBox)sender).NamingContainer);
            ////NamingContainer return the container that the control sits in
            //TextBox txt = (TextBox)row.FindControl("txt");
            //Label lbl = (Label)row.FindControl("lbl");
            //lbl.Text = "entered text: " + txt.Text;

            GridViewRow rows = (GridViewRow)((Control)sender).Parent.Parent;
            int index = rows.DataItemIndex;
            TextBox txt = (TextBox)gridfindcontrol.Rows[index].FindControl("txt");
            Label lbl = (Label)gridfindcontrol.Rows[index].FindControl("lbl");
            lbl.Text = "entered text: " + txt.Text;

        }
        protected void txtfoot_TextChanged(object sender, EventArgs e)
        {
            GridViewRow rows = gridfindcontrol.FooterRow;
            TextBox txt = (TextBox)rows.FindControl("txtfoot");
            Label lbl = (Label)rows.FindControl("lblfoot");
            lbl.Text = "entered text: " + txt.Text;

        }
        protected void gridfindcontrol_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl = (Label)e.Row.FindControl("lbl");
                if (lbl.Text == "DATA0")
                {
                    lbl.Text = "Intial";
                }
                else
                {
                    lbl.Text = "Not Intial";
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lbl = (Label)e.Row.FindControl("lblfoot");
                if (lbl.Text == "Label")
                {
                    lbl.Text = "Intial Footer";
                }
                else
                {
                    lbl.Text = "Not Intial Footer";
                }
            }
        }
        protected void gridfindcontrol_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OK")
            {
                Control ctrl = e.CommandSource as Control;

                if (ctrl != null)
                {
                    GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
                    Label lbl = (Label)_currenrtrow.FindControl("lbl");
                    lbl.Text = "You have Clicked :" + e.CommandArgument;
                }
            }
            else if (e.CommandName == "OKFOOT")
            {
                //Control ctrl = e.CommandSource as Control;

                //if (ctrl != null)
                //{
                //    GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
                //    Label lbl = (Label)_currenrtrow.FindControl("lblfoot");
                //    lbl.Text = "You have Clicked :" + e.CommandArgument;
                //}

                GridViewRow row = gridfindcontrol.FooterRow;
                Label lbl = (Label)row.FindControl("lblfoot");
                lbl.Text = "You have Clicked :" + e.CommandArgument;
            }
        }
        protected void btn_Click(object sender, EventArgs e)
        {
            int count = 0;
            foreach (GridViewRow gvr in gridfindcontrol.Rows)
            {
                Label lblsno = (Label)gvr.FindControl("lblsno");
                count += Convert.ToInt32(lblsno.Text);
            }

            lbltot.Text = count.ToString();
        }

        protected void gridfindcontrol_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = gridfindcontrol.Rows[e.RowIndex];
            Label tempname = (Label)row.FindControl("yourcontrolname");
        }
        protected void gridfindcontrol_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox temptxt = ((TextBox)(gridfindcontrol.Rows[e.RowIndex].FindControl("yourcontrolname")));
        }
        protected void gridfindcontrol_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewRow rows = gridfindcontrol.Rows[gridfindcontrol.EditIndex];
            int index = rows.DataItemIndex;
            TextBox temptxt = (TextBox)gridfindcontrol.Rows[index].FindControl("yourcontrolname");
        }


You Can Download the Working Code of Gridview FindControl in ASP.Net From here.