473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GridView dynamic footer row creation problem.

13 New Member
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_RowC ommand` event handler.
Dec 25 '09 #1
0 2704

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

Similar topics

3
13753
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
1
9356
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
2
2762
by: Flinky Wisty Pomm | last post by:
Hi all, I've got a really annoying problem that I need to fix sharpish. I've got a GridView derived control which has a templated header and footer. It works wonderfully on the first render but then the header/footer vanish into thin air. If I add the header/footer onDataBound then they disappear when I do a postback with no databinding. If I add them during PreRender, then they persist, but one of my data rows is emptied for each time I...
1
4976
by: Stu Lock | last post by:
Hi, I've spent the last hour trawling google for this - but all I find are people asking the same question! I have a gridview which is being databound to an empty datasource. I can display the 'EmptyDataText' no problem, but the header and footer do not display. The header I can live without - but the footer contains the 'Add new item' form elements, so needs to be visible when the grid is empty.
0
954
by: yossimotro | last post by:
Hi, I'm using a gridview in my project and its footer to add new records. The gridview is connected to an AccessDataSource which contains select, insert and delete commands. The problem occurs when the AccessDataSource is empty, the gridview won't show at all, not even the footer so I can't add new data. I know I can give up on the footer and just create somelike myself
3
11719
by: AG | last post by:
Below is code (slightly modified and converted to VB) that was provided to me in response to another post. I am using it to demonstrate another problem. In order for paging and other features to work properly in a gridview, viewstate must be enabled. So, in order to minimize the size of viewstate for a page, I will sometimes turn off viewstate for each control in template columns. That means that the gridview must be databound on each...
0
1798
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I have a gridview. I calculated some values in the gridview for the footer of the gridview so basically I am adding all the values that are displayed in the gridview for each column and displaying the total in the footer of the gridview. Now I export the gridview to excel spreadsheet with the code below. Everything is exported well except the footer value become zero in the excel spreadsheet. Can anyone tell me how can i...
0
2708
by: gnewsgroup | last post by:
Well, I am trying to use the footer row of a GridView for insertion purpose. There are some articles about this, for example, the gridviewguy.com has an example, which always displays the footer row. I would like to display the footer row, only when a LinkButton "Add New" is clicked. And after the new record is saved to the database, let the footer row disappear. In the LinkButton's click event handler, I simply say:
0
1664
by: tina2626 | last post by:
I m using this code in C#.net, for dynamic creation of GridView without using DB. <CODE> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Session = CreateDataTable();
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.