How to add a Text box in the Gridview Header-ASP.NET

In this code snippet, I will show you how to add control in this example textbox in ASP.NET GridView header control. Check out the following code snippet

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
          
            // Check the header type
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Literal litHeader;
                TextBox txtSearch;

                // loop through each cell
                for (int i = 0; i <= (e.Row.Cells.Count - 1); i++)
                {
                    litHeader = new Literal();
                    txtSearch = new TextBox();

                    // get the current header text
                    litHeader.Text = e.Row.Cells[i].Text;

                    // add the header text plus a new textbox
                    e.Row.Cells[i].Controls.Add(litHeader);
                    e.Row.Cells[i].Controls.Add(txtSearch);
                }

            }
        }

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru