By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

GridView dynamic footer row creation problem.

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

Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.             {
  3.                 CreateGridView();
  4.             }
  5.  
  6.             private void CreateGridView()
  7.             {
  8.                 GridView1.Columns.Clear();
  9.  
  10.                 DataTable dataTable = Book.GetBooksDataSet().Tables[0];
  11.  
  12.                 CommandField cf = new CommandField();
  13.                 cf.ShowEditButton = true;
  14.  
  15.                 GridView1.Columns.Add(cf);
  16.  
  17.                 int colCount = 1;
  18.                 foreach (DataColumn c in dataTable.Columns)
  19.                 {
  20.                     BoundField boundField = new BoundField();
  21.  
  22.                     boundField.DataField = c.ColumnName;
  23.                     boundField.HeaderText = c.ColumnName;
  24.                     //boundField.FooterText = "---";
  25.  
  26.                     if (colCount == 3 || colCount == 5)
  27.                     {
  28.                         boundField.ReadOnly = true;
  29.                     }
  30.  
  31.                     GridView1.Columns.Add(boundField);
  32.                     colCount++;
  33.                 }
  34.  
  35.                 GridView1.ShowFooter = true;
  36.  
  37.                 GridView1.DataSource = dataTable;
  38.                 GridView1.DataBind();
  39.  
  40.                 GridViewRow footerRow = GridView1.FooterRow;
  41.                 Button b = new Button();
  42.                 b.Text = "Add New";
  43.                 int i = 0;
  44.                 footerRow.Cells[i].Controls.Add(b);
  45.                 foreach (DataColumn c in dataTable.Columns)
  46.                 {
  47.                     ++i;
  48.                     TextBox tb = new TextBox();
  49.                     footerRow.Cells[i].Controls.Add(tb);
  50.                 }
  51.             }
  52.     ....................................
  53.     ....................................
  54.     ....................................
  55.     }
  56.  
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:


Expand|Select|Wrap|Line Numbers
  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  2.             {
  3.                 int index = Convert.ToInt32(e.CommandArgument);
  4.  
  5.                 if (e.CommandName == "Edit")
  6.                 {
  7.                     GridView1.EditIndex = index;
  8.  
  9.                     GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
  10.  
  11.                     //We can get cell data like this
  12.                     string id = selectedRow.Cells[1].Text;
  13.                     string isbn = selectedRow.Cells[2].Text;
  14.  
  15.                     //This is necessary to GridView to be showed up.
  16.                     CreateGridView();
  17.                 }
  18.                 else if (e.CommandName == "Update")
  19.                 {
  20.                     LinkButton updateButton = (LinkButton)e.CommandSource;
  21.  
  22.                     DataControlFieldCell dcfc = (DataControlFieldCell)updateButton.Parent;
  23.  
  24.                     GridViewRow gvr = (GridViewRow)dcfc.Parent;
  25.  
  26.                     //The update...................
  27.                     //Update grid-data to database
  28.                     UpdateDataInTheDatabase(gvr.Cells[1].Controls);                
  29.  
  30.                     //Grid goes back to normal
  31.                     GridView1.EditIndex = -1;
  32.  
  33.                     //This is necessary to GridView to be showed up.
  34.                     CreateGridView();
  35.                 }
  36.             }
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.
Dec 25 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.