Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 509665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:02:03+00:00 2026-05-13T07:02:03+00:00

I am able to create BoundFields and Footer-rows dynamically like this in my GridView:

  • 0

I am able to create BoundFields and Footer-rows dynamically like this in my GridView:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                CreateGridView();
            }
        }

        private void CreateGridView()
        {
            GridView1.Columns.Clear();

            DataTable dataTable = Book.GetBooksDataSet().Tables[0];

            CommandField cf = new CommandField();
            cf.ShowEditButton = true;

            GridView1.Columns.Add(cf);

            int colCount = 1;
            foreach (DataColumn c in dataTable.Columns)
            {
                BoundField boundField = new BoundField();

                boundField.DataField = c.ColumnName;
                boundField.HeaderText = c.ColumnName;
                //boundField.FooterText = "---";

                if (colCount == 3 || colCount == 5)
                {
                    boundField.ReadOnly = true;
                }

                GridView1.Columns.Add(boundField);
                colCount++;
            }

            GridView1.ShowFooter = true;

            GridView1.DataSource = dataTable;
            GridView1.DataBind();

            GridViewRow footerRow = GridView1.FooterRow;
            Button b = new Button();
            b.Text = "Add New";
            int i = 0;
            footerRow.Cells[i].Controls.Add(b);
            foreach (DataColumn c in dataTable.Columns)
            {
                ++i;
                TextBox tb = new TextBox();
                footerRow.Cells[i].Controls.Add(tb);
            }
        }
....................................
....................................
....................................
}

But the problem is, when I click the “Add New” – button, it disappears instantly. And, also I am unable to add any event handler to it. Or intercept its actions like this:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName == "Edit")
            {
                GridView1.EditIndex = index;

                GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];

                //We can get cell data like this
                string id = selectedRow.Cells[1].Text;
                string isbn = selectedRow.Cells[2].Text;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
            else if (e.CommandName == "Update")
            {
                LinkButton updateButton = (LinkButton)e.CommandSource;

                DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;

                GridViewRow gvr = (GridViewRow)dcfc.Parent;

                //The update...................
                //Update grid-data to database
                UpdateDataInTheDatabase(gvr.Cells[1].Controls);                

                //Grid goes back to normal
                GridView1.EditIndex = -1;

                //This is necessary to GridView to be showed up.
                CreateGridView();
            }
        }

One more thing, I have seen some solutions that suggests to handle the GridView’s rowBound event. But I need to do it from within Page_load event handler, or in, GridView1_RowCommand event handler.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:02:03+00:00Added an answer on May 13, 2026 at 7:02 am

    Dynamically created controls mus be re-created on every postback. Your “Add New” button causes a postback so the dynamically created footer disappears. Is there a reason this grid has to be created dynamically? From the code you posted it appears that you could do this in markup instead. If not, you’ll have to re-create the dynamic controls on every postback.

    Edited to add:
    I played with this a little bit and what’s below works in that the grid doesn’t disappear and events are handled, but it doesn’t actually do anything. Hope this helps.

    Markup:

        <p><asp:Literal ID="Literal1" runat="server" /></p>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
            OnRowCommand="GridView1_RowCommand" 
            OnRowEditing="GridView1_RowEditing"/>
    

    Code:

    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridView();
    }
    
    private DataTable GetBooksDataTable()
    {
        var dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Title", typeof(string));
        dt.Columns.Add("Author", typeof(string));
    
        for (int index = 0; index < 10; index++)
        {
            dt.Rows.Add(index, "Title" + index, "Author" + index);
        }
        return dt;
    }
    
    private void BindGridView()
    {
        var dt = GetBooksDataTable();
    
        GridView1.Columns.Clear();
        GridView1.ShowFooter = true;
    
        var cf = new CommandField();
        cf.HeaderText = "Action";
        cf.ShowEditButton = true;
        GridView1.Columns.Add(cf);
    
        for (int index = 0; index < dt.Columns.Count; index++)
        {
            var boundField = new BoundField();
            boundField.DataField = dt.Columns[index].ColumnName;
            boundField.HeaderText = dt.Columns[index].ColumnName;
            GridView1.Columns.Add(boundField);
        }
    
        GridView1.DataSource = dt;
        GridView1.DataBind();
    
        var footer = GridView1.FooterRow;
        var b = new LinkButton();
        b.Text = "Add New";
        b.CommandName = "Add New";
        footer.Cells[0].Controls.Add(b);
        for (int index = 1; index < dt.Columns.Count + 1; index++)
        {
            var tb = new TextBox();
            footer.Cells[index].Controls.Add(tb);
        }
    
    }
    
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Literal1.Text = e.CommandName;
    }
    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.