Geeks With Blogs

News
Ivan Porto Carrero Placeholder.Add("Really Cool Stuff");

Natively the GridView doesn't support the insertion of records.  However you can manipulate the footerrow in such a way that you can use it as an insertion row.  These are the steps :

1. Enable your gridview to show footers.
2. Make all editable columns and the command column into templatefields
3. Add the appropriate Textboxes and validationcontrols to the footer template of each column; name all the textboxes.
4. Add an Insert button to the column containing the buttons. Set it's command to Insert and make an eventhandler for the click event.
5. Add an onInserting eventhandler to your datasourcecontrol

I just post the source from my designer and the relevant eventhandlers to this article, the rest is up to you.

<asp:GridView ID="gvIngredient" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductIngredient_ID" DataSourceID="odsIngredient" ShowFooter="True" Width="792px" OnSelectedIndexChanged="gvIngredient_SelectedIndexChanged" OnRowCreated="gvIngredient_RowCreated">

    <Columns>

            <asp:TemplateField ShowHeader="False">

            <ItemTemplate>

                <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="Edit” 
                Text="Edit" >asp:LinkButton>  <asp:LinkButton ID="lbDelete" runat="server"

                CausesValidation="False" CommandName="Delete" Text="Delete">asp:LinkButton>

            ItemTemplate>

            <EditItemTemplate>

                <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="Update"

                Text="Update" >asp:LinkButton> <asp:LinkButton ID="lbCancel" runat="server" 

                CausesValidation="False" CommandName="Cancel" Text="Cancel">asp:LinkButton>

            EditItemTemplate>

            <FooterTemplate>

                <asp:LinkButton ID="lbInsert" runat="server" CommandName="Insert" ValidationGroup="addVal"
                        OnClick="lbInsert_Click">Insertasp:LinkButton> <asp:LinkButton ID="LinkButton2"
                        runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"
                        OnClick="LinkButton2_Click">asp:LinkButton>

            FooterTemplate>

        asp:TemplateField>

        <asp:TemplateField HeaderText="Ingredient" SortExpression="ProductIngredient_Name">

            <ItemTemplate>

    <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductIngredient_Name") %>' >
           asp:Label>

            ItemTemplate>

            <EditItemTemplate>

                <asp:TextBox ID="tbIngredient" runat="server" Text='<%# Bind("ProductIngredient_Name") %>'>
                        asp:TextBox>

            EditItemTemplate>

            <FooterTemplate>

                <asp:TextBox ID="tbIngredient" runat="server" Text='<%# Bind("ProductIngredient_Name") %>'>

                        asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

                ControlToValidate="tbIngredient" ErrorMessage="Enter an ingredient" ValidationGroup="addVal">

                asp:RequiredFieldValidator>

            FooterTemplate>

        asp:TemplateField>

        <asp:TemplateField HeaderText="Stock" SortExpression="ProductIngredient_Stock">

            <ItemTemplate>

                <asp:Label ID="Label2" runat="server" Text='<%# Bind("ProductIngredient_Stock") %>'>asp:Label>

            ItemTemplate>

            <EditItemTemplate>

                <asp:TextBox ID="tbStock" runat="server" Text='<%# Bind("ProductIngredient_Stock") %>'

                Width="49px"> asp:TextBox>

            EditItemTemplate>

            <FooterTemplate>

                <asp:TextBox ID="tbStock" runat="server" Text='<%# Bind("ProductIngredient_Stock") %>'

                Width="49px">asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2"

                runat="server" ControlToValidate="tbStock" ErrorMessage="Enter a stocklevel"

                ValidationGroup="addVal">asp:RequiredFieldValidator>

            FooterTemplate>

        asp:TemplateField>

        <asp:TemplateField HeaderText="Minimum stock" SortExpression="ProductIngredient_MinStock">

            <ItemTemplate>

                <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductIngredient_MinStock") %>'>

                asp:Label>

            ItemTemplate>

            <EditItemTemplate>

                <asp:TextBox ID="MinStock" runat="server" Text='<%# Bind("ProductIngredient_MinStock") %>'

                Width="56px">asp:TextBox>

            EditItemTemplate>

            <FooterTemplate>

                <asp:TextBox ID="tbMinStock" runat="server" Text='<%# Bind("ProductIngredient_MinStock") %>'

                Width="56px">asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator3"

                runat="server" ControlToValidate="tbMinStock" ErrorMessage="Enter a minimum stock"

                ValidationGroup="addVal">asp:RequiredFieldValidator>

            FooterTemplate>

        asp:TemplateField>
    Columns>

asp:GridView>

<asp:ObjectDataSource ID="odsIngredient" runat="server" DeleteMethod="Delete" InsertMethod="Insert" SelectMethod="GetData" TypeName="SMAP.Datalogic.dsSMAPTableAdapters.ProductIngredientTableAdapter" UpdateMethod="Update" OnDeleting="odsIngredient_Deleting" OnInserting="odsIngredient_Inserting">

            <DeleteParameters>

                    <asp:Parameter Name="ProductIngredient_Deleted" Type="Boolean" />

                    <asp:Parameter Name="Original_ProductIngredient_ID" />

            DeleteParameters>

            <UpdateParameters>

                    <asp:Parameter Name="ProductIngredient_Name" Type="String" />

                    <asp:Parameter Name="ProductIngredient_Stock" Type="Double" />

                    <asp:Parameter Name="ProductIngredient_MinStock" Type="Double" />

                    <asp:Parameter Name="Original_ProductIngredient_ID" />

            UpdateParameters>

            <SelectParameters>

                    <asp:ControlParameter ControlID="cbDeleted" Name="ProductIngredient_Deleted"

                    PropertyName="Checked" Type="Boolean" />

            SelectParameters>

            <InsertParameters>

                    <asp:Parameter Name="ProductIngredient_Name" Type="String" />

                    <asp:Parameter Name="ProductIngredient_Stock" Type="Double" />

                    <asp:Parameter Name="ProductIngredient_MinStock" Type="Double" />

            InsertParameters>

asp:ObjectDataSource>

protected void lbInsert_Click(object sender, EventArgs e)

   {

      odsIngredient.Insert();

   }

   protected void LinkButton2_Click(object sender, EventArgs e)

   {

      ((TextBox)gvIngredient.FooterRow.FindControl("tbIngredient")).Text = "";

      ((TextBox)gvIngredient.FooterRow.FindControl("tbStock")).Text = "";

      ((TextBox)gvIngredient.FooterRow.FindControl("tbMinStock")).Text = "";

   }

   protected void odsIngredient_Inserting(object sender, ObjectDataSourceMethodEventArgs e)

   {

      string Ingredient = ((TextBox)gvIngredient.FooterRow.FindControl("tbIngredient")).Text;

      string stock = ((TextBox)gvIngredient.FooterRow.FindControl("tbStock")).Text;

      string minstock = ((TextBox)gvIngredient.FooterRow.FindControl("tbMinStock")).Text;

      e.InputParameters["ProductIngredient_Name"] = Ingredient;

      e.InputParameters["ProductIngredient_Stock"] = stock;

      e.InputParameters["ProductIngredient_MinStock"] = minstock;

   }

Let me know if this article was helpful to somebody.

This only works if there is already data in the gridview. You need at least one row of data displayed in your gridview.

I've put an updated version, you don't need the click event hanler at all and it handles the emptydatatemplate also, online here :
http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx

 

Posted on Monday, August 29, 2005 3:20 PM | Back to top

Related Posts on Geeks With Blogs Matching Categories
Copyright © Ivan Porto Carrero | Powered by: GeeksWithBlogs.net