Saturday, July 4, 2009

Web Application: Display GridView Footer - When no records in the Gridview

The following article describes how to display a Gridview Footer regardless of number of rows in the Gridview.

The footer in the Gridview is normally used as inserting interface to add records to the GridView.

Well! this concept works very well as long there are more than 1 number or rows in the Gridview.

But the footer will not get displayed when there are no rows in the GridView.

In ASP.NET 1.x this issue was not present as the footer used to display always regardless of the number of rows in the Gridview.

In ASP.NET 2.x, that is not the case. The footer will work only in presence of rows in the Gridview.

But what we can do if we want the inserting interface with no rows in the Gridview.

The option is to use EmptyDataTemplate Option of the ASP GridView Control.

Also we can make good use of Details view.

Steps:

1. Place Details View in the EmptyDateTemplate of GridView.

2. Bind it to the same data source as Gridview

3. Set the Enable Inserting option from its Smart Tag

4. Set the Default Mode Property to Insert.

Now the inserting interface is ready.

Once they add the first record, the GridView will display its new, single record and future records can be added through the Footer Row.


Handling of Duplicate Data Binding

Here, we have binded Details View against the Same Data Source, so for the very first record, it will not be a problem. But for other first record, it will create duplicate entries.

To avoid this we need to Check the count of rows. If it is greater than 0, then we will add , otherwise just returning from the function without adding(Details View will be used).


Consider the following example(c#):

This example adds records to Products table. The GridView is bounded to Products DataSource(dsProducts)

Details View inside EmptyDataTemplate of Gridview




< asp:GridView >
< EmptyDataTemplate >
< asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="dsProducts"
DefaultMode="Insert" >
< Fields >
< asp:TemplateField HeaderText="ProductID" InsertVisible="False" SortExpression="ProductID" >
< InsertItemTemplate >
< asp:Button ID="AddProduct" runat="server" CommandName="Insert" Text="Add" / >
< /InsertItemTemplate >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Product" SortExpression="ProductName" >
< InsertItemTemplate >
< asp:TextBox ID="NewProductName" runat="server" Text='< %# Bind("ProductName") % >' >< /asp:TextBox >
< asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="NewProductName"
Display="Dynamic" ErrorMessage="You must enter a name for the new product." ForeColor="" >*< /asp:RequiredFieldValidator >
< /InsertItemTemplate >
< FooterStyle Wrap="False" / >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Category" SortExpression="CategoryName" >
< InsertItemTemplate >
< asp:DropDownList ID="NewCategoryID" runat="server" DataSourceID="CategoriesDataSource"
DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='< %# Bind("CategoryID") % >' >
< /asp:DropDownList >< asp:ObjectDataSource ID="CategoriesDataSource" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories" TypeName="CategoriesBLL" >
< /asp:ObjectDataSource >
< /InsertItemTemplate >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Supplier" SortExpression="SupplierName" >
< InsertItemTemplate >
< asp:DropDownList ID="NewSupplierID" runat="server" DataSourceID="SuppliersDataSource"
DataTextField="CompanyName" DataValueField="SupplierID" SelectedValue='< %# Bind("SupplierID") % >' >
< /asp:DropDownList >< asp:ObjectDataSource ID="SuppliersDataSource" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetSuppliers" TypeName="SuppliersBLL" >
< /asp:ObjectDataSource >
< /InsertItemTemplate >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Qty/Unit" SortExpression="QuantityPerUnit" >
< InsertItemTemplate >
< asp:TextBox ID="NewQuantityPerUnit" runat="server" Text='< %# Bind("QuantityPerUnit") % >' >< /asp:TextBox >
< /InsertItemTemplate >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Price" SortExpression="UnitPrice" >
< InsertItemTemplate >
$< asp:TextBox ID="NewUnitPrice" runat="server" Columns="8" Text='< %# Bind("UnitPrice") % >' >< /asp:TextBox >
< asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="NewUnitPrice"
ErrorMessage="You must enter a valid currency value greater than or equal to 0.00. Do not include the currency symbol."
ForeColor="" Operator="GreaterThanEqual" Type="Currency" ValueToCompare="0" Display="Dynamic" >*< /asp:CompareValidator >
< /InsertItemTemplate >
< FooterStyle Wrap="False" / >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Units In Stock" SortExpression="Units In Stock" >
< InsertItemTemplate >
< asp:TextBox ID="NewUnitsInStock" runat="server" Columns="5" Text='< %# Bind("UnitsInStock") % >' >< /asp:TextBox >
< asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="NewUnitsInStock"
Display="Dynamic" ErrorMessage="You must enter a valid numeric value for units in stock that's greater than or equal to zero."
ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0" >*< /asp:CompareValidator >
< /InsertItemTemplate >
< FooterStyle Wrap="False" / >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Units On Order" SortExpression="UnitsOnOrder" >
< InsertItemTemplate >
< asp:TextBox ID="NewUnitsOnOrder" runat="server" Columns="5" Text='< %# Bind("UnitsOnOrder") % >' >< /asp:TextBox >
< asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="NewUnitsOnOrder"
Display="Dynamic" ErrorMessage="You must enter a valid numeric value for units on order that's greater than or equal to zero."
ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0" >*< /asp:CompareValidator >
< /InsertItemTemplate >
< FooterStyle Wrap="False" / >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Reorder Level" SortExpression="ReorderLevel" >
< InsertItemTemplate >
< asp:TextBox ID="NewReorderLevel" runat="server" Columns="5" Text='< %# Bind("ReorderLevel") % >' >< /asp:TextBox >
< asp:CompareValidator ID="CompareValidator4" runat="server" ControlToValidate="NewReorderLevel"
Display="Dynamic" ErrorMessage="You must enter a valid numeric value for reorder level that's greater than or equal to zero."
ForeColor="" Operator="GreaterThanEqual" Type="Integer" ValueToCompare="0" >*< /asp:CompareValidator >
< /InsertItemTemplate >
< FooterStyle Wrap="False" / >
< /asp:TemplateField >
< asp:TemplateField HeaderText="Discontinued" SortExpression="Discontinued" >
< InsertItemTemplate >
< asp:CheckBox ID="NewDiscontinued" runat="server" Checked='< %# Bind("Discontinued") % >' / >
< /InsertItemTemplate >
< /asp:TemplateField >
< asp:CommandField ShowInsertButton="True" / >
< /Fields >
< /asp:DetailsView >
< /EmptyDataTemplate >
< /asp:GridView >






//This code is inserting record, when they click on Insert Button

protected void dgProductsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
// Insert data if the CommandName == "Insert" and the validation controls indicate valid data...
if (e.CommandName == "Insert" && Page.IsValid)
{
// Insert new record
dsProducts.Insert();

}
}



//This code handles the actual insertion of records
//Retrieves the data from the Footer and record to the database

protected void dsProducts_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{

// Only perform the following logic when inserting through the footer
if (dgProductsGrid.Rows.Count == 0)
// We are inserting through the DetailsView in the EmptyDataTemplate
return;

// Programmatically reference Web controls in the inserting interface...
TextBox NewProductName = (TextBox)Products.FooterRow.FindControl("NewProductName");
DropDownList NewCategoryID = (DropDownList)Products.FooterRow.FindControl("NewCategoryID");
DropDownList NewSupplierID = (DropDownList)Products.FooterRow.FindControl("NewSupplierID");
TextBox NewQuantityPerUnit = (TextBox)Products.FooterRow.FindControl("NewQuantityPerUnit");
TextBox NewUnitPrice = (TextBox)Products.FooterRow.FindControl("NewUnitPrice");
TextBox NewUnitsInStock = (TextBox)Products.FooterRow.FindControl("NewUnitsInStock");
TextBox NewUnitsOnOrder = (TextBox)Products.FooterRow.FindControl("NewUnitsOnOrder");
TextBox NewReorderLevel = (TextBox)Products.FooterRow.FindControl("NewReorderLevel");
CheckBox NewDiscontinued = (CheckBox)Products.FooterRow.FindControl("NewDiscontinued");

// Set the ObjectDataSource's InsertParameters values...
e.InputParameters["productName"] = NewProductName.Text;

e.InputParameters["supplierID"] = Convert.ToInt32(NewSupplierID.SelectedValue);
e.InputParameters["categoryID"] = Convert.ToInt32(NewCategoryID.SelectedValue);

string quantityPerUnit = null;
if (!string.IsNullOrEmpty(NewQuantityPerUnit.Text))
quantityPerUnit = NewQuantityPerUnit.Text;
e.InputParameters["quantityPerUnit"] = quantityPerUnit;

decimal? unitPrice = null;
if (!string.IsNullOrEmpty(NewUnitPrice.Text))
unitPrice = Convert.ToDecimal(NewUnitPrice.Text);
e.InputParameters["unitPrice"] = unitPrice;

short? unitsInStock = null;
if (!string.IsNullOrEmpty(NewUnitsInStock.Text))
unitsInStock = Convert.ToInt16(NewUnitsInStock.Text);
e.InputParameters["unitsInStock"] = unitsInStock;

short? unitsOnOrder = null;
if (!string.IsNullOrEmpty(NewUnitsOnOrder.Text))
unitsOnOrder = Convert.ToInt16(NewUnitsOnOrder.Text);
e.InputParameters["unitsOnOrder"] = unitsOnOrder;

short? reorderLevel = null;
if (!string.IsNullOrEmpty(NewReorderLevel.Text))
reorderLevel = Convert.ToInt16(NewReorderLevel.Text);
e.InputParameters["reorderLevel"] = reorderLevel;

e.InputParameters["discontinued"] = NewDiscontinued.Checked;
}




This line if (dgProductsGrid.Rows.Count == 0) avoids the duplicate entry.

Thus we can add rows regardless of number of rows in the GridView

No comments:

Post a Comment