Scott on Writing

Musings on technical writing...

Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid!

As I mentioned in an earlier blog post this month, three new data tutorials were added to my Working with Data in ASP.NET 2.0 tutorial series. One of these new tutorials looked at how to use the GridView's footer row as an inserting interface (see Inserting a New Record from the GridView's Footer [VB | C#]). The “footer as inserting interface” approach works well enough if there are records bound to the GridView. However, if you are working with a GridView that has no data associated with it, the footer row doesn't appear. In ASP.NET 1.x this wasn't an issue because regardless of whether there were any records in the DataGrid's data source, the header and footer were still displayed. It was the page developer's responsibility to check if there were no data records bound to the DataGrid and, if so, to hide the grid and display an appropriate message. In the case of using the footer to insert, however, we would leave the DataGrid visible so that the footer row was still present to allow the user to add the first record to the grid.

The GridView in ASP.NET 2.0 works a bit differently. If there are no records in the GridView's data source, it does not display its header or footer. If its EmptyDataText or EmptyDataTemplate properties are set, it will show that, otherwise the GridView emits no markup.

So, if you have an inserting interface in the GridView's footer, but there are no records in the underlying table, the footer will not display! Eep. What to do?

One option is to use the EmptyDataTemplate property. Drop a DetailsView in there, bind it to the same data source control as the GridView, check the “Enable Inserting” option from its smart tag and set its DefaultMode property to Insert. This will present the user with an inserting interface when there are no records in the table. Once they add a new record, the GridView will display its new, single record and future records can be added through the footer row.

If you use the DetailsView in the EmptyDataTemplate option and bind it to the same ObjectDataSource as used by the GridView with the code presented in the tutorial, you'll need to make one minor code modification. The ObjectDataSource has an event handler for its Inserting event. There is picks out the values from the footer and uses those values to populate its parameters. When inserting from the DetailsView, however, we can use two-way databinding instead (i.e., adding <%# Bind(”ColumnName”) %> to the properties of the Web controls in the DetailsView's InsertItemTemplates). Consequently, you'll need to bypass reading in the GridView's footer row values when inserting from the DetailsView, which can be accomplished with the following conditional statement:

    1 // Only performt he following logic when inserting through the footer

    2 if (Products.Rows.Count == 0)

    3     // We are inserting through the DetailsView in the EmptyDataTemplate

    4     return;

I've got a working example you can download to test this out. It's a stripped-down version of the download from the tutorial. I've manually deleted all of the rows from the Products table and added a DetailsView to the GridView's EmptyDataTemplate. Moreover, I've bound this DetailsView to the same ObjectDataSource as used by the GridView, added the above conditional statement to the ObjectDataSource's Inserting event handler, and added the markup to implement two-way databinding on the DetailsView. You can download this example from: http://datawebcontrols.com/demos/InsertFromFooter.zip

Hope this helps and, as always, Happy Programming!

posted on Friday, March 16, 2007 2:55 PM

Feedback

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 3/20/2007 9:46 AM Nalini

Thank you and this is what i was looking for . The way i implemented this was by adding buch of textboxes and dropdowns in a table in EmptyDataTemplate.

Thank you very much again for your great tutorials.

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 4/4/2007 7:34 AM Raja

The datasource of the gridview changes dynamically, so I have to change the datasource of the detailsview in the emptydata template also dynamically. How can I do that?

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 4/4/2007 2:50 PM Raja

All the template columns in the gridview are created dynamically except the first column that contains the edit, delete and select link buttons.Any click event on these buttons is removing all the template columns.So i am not able to access the new values in the grid_updating,grid_rowcammand events.How can i fix this. Thank you in advance.

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 4/22/2007 7:41 AM Céderic

I searched this for so lang but this does the trick!!!

Add a button to yhou empty template en let it do this.


protected void Button1_Click(object sender, EventArgs e)
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
}

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 4/4/2008 12:00 AM 309840

I still dun noe how to do it...please elaborate on how to use a SQL datasource to do it

# In-Depth look at the GridView control bound to an AccessDataSource 1/22/2009 7:56 PM Patrice Paré

Recently, I worked on a small home project. This project objective was to manage of a condominium. Since

# re: Inserting through the GridView's Footer Row - Easy Unless There are No Records Bound to the Grid! 4/21/2009 3:22 AM Faheem

If gridview is under contentplaceholder ,code is not working for me.Its not taking values from footer textboxes.

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles