Search
Close this search box.

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 : http://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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <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" />&nbsp;
               <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" />&nbsp;
               <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" />&nbsp;
               <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><br />
               <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 + "<br />";

    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 + "<br />";

      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 : <b>{0}</b> 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 :  <b>{0}</b> from the footerrow",
          tbInsert.Text);

      64
    }

    65
  }

  66

      67
}

posted on Thursday, May 04, 2006 11:32 AM

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

Related Posts