Home > ASP.Net > Show Header when the Gridview is Empty

Show Header when the Gridview is Empty


Hi

try this example:

ASPX

<%@ page autoeventwireup="true" codefile="AddInsertRow.aspx.cs" inherits="GridView_AddInsertRow"
 language="C#" masterpagefile="~/MasterPages/Default.master" title="GridView: Add Insert Row" %>

<asp:content id="Content1" runat="Server" contentplaceholderid="ContentPlaceHolder1">
 <asp:label id="lblResults" runat="server" />
 <asp:gridview id="gvSuppliers" runat="server" allowpaging="True" autogeneratecolumns="False"
  datakeynames="ShipperID" datasourceid="sdsSuppliers" emptydatatext="There are no data records to display."
  onrowcommand="gvSuppliers_RowCommand" onrowcreated="gvSuppliers_RowCreated" onrowdeleted="gvSuppliers_RowDeleted"
  onrowupdated="gvSuppliers_RowUpdated" showfooter="true" style="margin-top: 12px;">
  <emptydatatemplate>
   <div style="float: left; width: 100px;">
    <asp:linkbutton id="btnInsert" runat="server" commandname="EmptyDataTemplateInsert"
     text="Insert" />
   </div>
   <div style="float: left; width: 175px;">
    <asp:textbox id="txtCompanyName" runat="server" />
   </div>
   <div style="float: right;">
    <asp:textbox id="txtPhone" runat="server" />
   </div>
  </emptydatatemplate>
  <columns>
   <asp:templatefield headertext="Action">
    <itemtemplate>
     <asp:linkbutton id="btnEdit" runat="server" causesvalidation="false" commandname="Edit"
      text="Edit" />
     <asp:linkbutton id="btnDelete" runat="server" causesvalidation="false" commandname="Delete"
      text="Delete" />
    </itemtemplate>
    <edititemtemplate>
     <asp:linkbutton id="btnUpdate" runat="server" commandname="Update" text="Update" />
     <asp:linkbutton id="btnCancel" runat="server" causesvalidation="false" commandname="Cancel"
      text="Cancel" />
    </edititemtemplate>
    <footertemplate>
     <asp:linkbutton id="btnInsert" runat="server" commandname="FooterInsert" text="Insert" />
    </footertemplate>
   </asp:templatefield>
   <asp:boundfield datafield="ShipperID" headertext="ShipperID" readonly="True" sortexpression="ShipperID" />
   <asp:templatefield headertext="Company Name">
    <itemtemplate>
     <%# Eval("CompanyName") %>
    </itemtemplate>
    <edititemtemplate>
     <asp:textbox id="txtCompanyName" runat="server" text='<%# Bind("CompanyName") %>' />
    </edititemtemplate>
    <footertemplate>
     <asp:textbox id="txtCompanyName" runat="server" />
    </footertemplate>
   </asp:templatefield>
   <asp:templatefield headertext="Phone">
    <itemtemplate>
     <%# Eval("Phone") %>
    </itemtemplate>
    <edititemtemplate>
     <asp:textbox id="txtPhone" runat="server" text='<%# Bind("Phone") %>' />
    </edititemtemplate>
    <footertemplate>
     <asp:textbox id="txtPhone" runat="server" />
    </footertemplate>
   </asp:templatefield>
  </columns>
 </asp:gridview>
 <asp:sqldatasource id="sdsSuppliers" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
  deletecommand="DELETE FROM [Shippers] WHERE [ShipperID] = @ShipperID" insertcommand="INSERT INTO [Shippers] ([CompanyName], [Phone]) VALUES (@CompanyName, @Phone)"
  oninserted="sdsSuppliers_Inserted" selectcommand="SELECT * FROM [Shippers]" updatecommand="UPDATE [Shippers] SET [CompanyName] = @CompanyName, [Phone] = @Phone WHERE [ShipperID] = @ShipperID">
  <insertparameters>
   <asp:parameter name="CompanyName" type="String" />
   <asp:parameter name="Phone" type="String" />
  </insertparameters>
  <updateparameters>
   <asp:parameter name="CompanyName" type="String" />
   <asp:parameter name="Phone" type="String" />
   <asp:parameter name="ShipperID" type="Int32" />
  </updateparameters>
  <deleteparameters>
   <asp:parameter name="ShipperID" type="Int32" />
  </deleteparameters>
 </asp:sqldatasource>
</asp:content>

CODE-BEHIND

using System.Web.UI.WebControls;

public partial class GridView_AddInsertRow : System.Web.UI.Page
{
 protected void gvSuppliers_RowCreated(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.EmptyDataRow)
  {
   // Create header row
   GridViewRow gvr = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);

   TableCell cell = new TableCell();
   cell.Text = "Action";
   gvr.Cells.Add(cell);

   cell = new TableCell();
   cell.Text = "Company Name";
   gvr.Cells.Add(cell);

   cell = new TableCell();
   cell.Text = "Phone";
   gvr.Cells.Add(cell);

   // Add row
   gvSuppliers.Controls[0].Controls.AddAt(0, gvr);
  }
 }

 protected void gvSuppliers_RowUpdated(object sender, GridViewUpdatedEventArgs e)
 {
  if ((e.Exception == null) && e.AffectedRows.Equals(1))
  {
   lblResults.Text = "Supplier successfully updated.";
  }
  else
  {
   lblResults.Text = "Unable to successfully update supplier.";
   e.ExceptionHandled = true;
  }
 }

 protected void gvSuppliers_RowDeleted(object sender, GridViewDeletedEventArgs e)
 {
  if ((e.Exception == null) && e.AffectedRows.Equals(1))
  {
   lblResults.Text = "Supplier successfully deleted.";
  }
  else
  {
   lblResults.Text = "Unable to successfully delete supplier.";
   e.ExceptionHandled = true;
  }
 }

 protected void gvSuppliers_RowCommand(object sender, GridViewCommandEventArgs e)
 {
  if (e.CommandName.Equals("EmptyDataTemplateInsert"))
  {
   // Retrieve row
   GridViewRow gvr = gvSuppliers.Controls[0].Controls[1] as GridViewRow;

   if (gvr == null) { return; }
   
   // Retrieve controls
   TextBox txtCompanyName = gvr.FindControl("txtCompanyName") as TextBox;
   TextBox txtPhone = gvr.FindControl("txtPhone") as TextBox;

   if (txtCompanyName == null) { return; }
   if (txtPhone == null) { return; }

   // Set parameters
   sdsSuppliers.InsertParameters["CompanyName"].DefaultValue = txtCompanyName.Text;
   sdsSuppliers.InsertParameters["Phone"].DefaultValue = txtPhone.Text;

   // Perform insert
   sdsSuppliers.Insert();
  }
  else if (e.CommandName.Equals("FooterInsert"))
  {
   // Retrieve controls
   TextBox txtCompanyName = gvSuppliers.FooterRow.FindControl("txtCompanyName") as TextBox;
   TextBox txtPhone = gvSuppliers.FooterRow.FindControl("txtPhone") as TextBox;

   if (txtCompanyName == null) { return; }
   if (txtPhone == null) { return; }

   // Set parameters
   sdsSuppliers.InsertParameters["CompanyName"].DefaultValue = txtCompanyName.Text;
   sdsSuppliers.InsertParameters["Phone"].DefaultValue = txtPhone.Text;

   // Perform insert
   sdsSuppliers.Insert();
  }
 }
 
 protected void sdsSuppliers_Inserted(object sender, SqlDataSourceStatusEventArgs e)
 {
  if ((e.Exception == null) && e.AffectedRows.Equals(1))
  {
   lblResults.Text = "Supplier successfully added.";
  }
  else
  {
   lblResults.Text = "Unable to successfully add supplier.";
   e.ExceptionHandled = true;
  }
 }
}

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: