Adding insert capabilities to the gridview

I didn't have time to wrap it in a proper control yet. In this post I'll just be putting the page implementation.

The gridview is cool and to add an insertrow to it can be done by the footer template. How to do it I explained a while ago: https://geekswithblogs.net/casualjim/articles/51360.aspx

If you want to get the controls from the footerrow you will need to address them with GridView.FooterRow.FindControl("ControlName");.

For the empty datatemplate it's a little bit trickier but not that much. It still is a gridviewrow but it's wrapped in another control. If you use a button control in your empty datatemplate you also can't use the submit behaviour from then on everything should be familiar.:)

This is the part in the page:

<%@ Page Language="C#" Theme="" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">       <head id="Head1" runat="server">         <title>ASP.NET Insert data in Gridview </title>       </head> <body>   <form id="form1" runat="server">      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>      <asp:GridView ID="GridView1" ShowFooter="true" runat="server"             OnRowCommand="GridView1_RowCommand1" AutoGenerateColumns="false">      <Columns>         asp:TemplateField\            <ItemTemplate>               <asp:Button Text="Edit" CommandName="Edit" CausesValidation="false" runat="server" ID="btEdit" />                 <asp:Button Text="Delete" CommandName="Delete" CausesValidation="false" runat="server" ID="btDelete" />            </ItemTemplate>            <EditItemTemplate>               <asp:Button Text="Update" CommandName="Update" CausesValidation="true" runat="server" ID="btUpdate" />                <asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />            </EditItemTemplate>            <FooterTemplate>               <asp:Button Text="Insert" CommandName="Insert" CausesValidation="true" runat="server" ID="btInsert" />                <asp:Button Text="Cancel" CommandName="Cancel" CausesValidation="false" runat="server" ID="btCancel" />            </FooterTemplate>         </asp:TemplateField>         <asp:TemplateField >            <ItemTemplate>               <asp:Label ID="lblValue" Text='<%# Eval("Name") %>' runat="server"></asp:Label>            </ItemTemplate>            <EditItemTemplate>                  <asp:TextBox ID="tbUpdate" runat="server" Text='<% Bind("Name") %>'></asp:TextBox>            </EditItemTemplate>            <FooterTemplate>                  <asp:TextBox ID="tbInsert" runat="server" Text="" ></asp:TextBox>            </FooterTemplate>             </asp:TemplateField>          </Columns>          <EmptyDataTemplate>                <asp:TextBox ID="tbEmptyInsert" runat="server"></asp:TextBox>
               <asp:Button ID="btSend" Text="Insert" runat="server" CommandName="EmptyInsert" UseSubmitBehavior="False" />             </EmptyDataTemplate>          </asp:GridView>       </form>    </body> </html>

And the code behind:

   14 public partial class Test: System.Web.UI.Page

   15 {

   16     protected void Page_Load(object sender, EventArgs e)

   17     {

   18         if (!IsPostBack)

   19         {

   20             //Create dummy data

   21             DataTable dt = new DataTable;

   22             DataColumn dc = new DataColumn("Name");

   23             dt.Columns.Add(dc);

   24             DataRow dr = dt.NewRow;

   25             dr["Name"] = "Ivan";

   26 

   27             //Uncomment the following line to have data in the grid:)

   28             //dt.Rows.Add(dr);

   29 

   30             //Bind the gridview

   31             GridView1.DataSource = dt;

   32             GridView1.DataBind;

   33         }

   34         //Recurses through the controls to show the naming of each individual control that is currently in the gridview

   35         RecurseControls(GridView1.Controls[0].Controls);

   36         Label1.Text += GridView1.Controls[0].Controls[0].GetType.Name + "
"; 

   37     }

   38 

   39     void RecurseControls(ControlCollection ctls)

   40     {

   41         foreach (Control ctl in ctls)

   42         {

   43             if (!ctl.HasControls)

   44                 Label1.Text += ctl.ClientID + " " + ctl.GetType.Name + "
";

   45             else

   46                 RecurseControls(ctl.Controls);

   47         }

   48     }

   49 

   50     protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)

   51     {

   52         if (e.CommandName == "EmptyInsert")

   53         {

   54             //handle insert here

   55             TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl("tbEmptyInsert") as TextBox;

   56             Label1.Text = string.Format("You would have inserted the name: {0} from the emptydatatemplate",tbEmptyInsert.Text);

   57 

   58         }

   59         if (e.CommandName == "Insert")

   60         {

   61             //handle insert here

   62             TextBox tbInsert = GridView1.FooterRow.FindControl("tbInsert") as TextBox;

   63             Label1.Text = string.Format("You would have inserted the name:  {0} from the footerrow", tbInsert.Text);

   64         }

   65     }

   66 

   67 }

!

This article is part of the GWB Archives. Original Author: Ivan Porto Carrero

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.