Gridview Insert Command

Last post 05-15-2009 8:40 AM by Anto.NET. 7 replies.

Sort Posts:

  • Tongue Tied [:S] Gridview Insert Command

    05-27-2005, 4:45 PM
    • Member
      180 point Member
    • mauricioaglr
    • Member since 05-11-2005, 5:12 AM
    • Los Angeles
    • Posts 36
    Hello,

    I am trying to get the gridview command to give me the insert option. All the other options are available like paging, sorting, edit, and delete but not insert. If I say edit column and edit he commandField I could say yes in the showInsertButton and I could see a new link in the datagrid but it does not work. I could edit and delete with a problem. Another problem If I could get this to work how can I create a blank row in the datagrid to insert a new record, like in a tabular MS Access form? Please help.
    Mauricio

    "Whatsoever ye would that men do unto you, do you even so unto them" (Mathew 7:12)
  • Re: Gridview Insert Command

    05-30-2005, 4:41 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    Hello.

    the gridview doesn't support the insertion of records. if you wan to insert a new item you should use the detailsview or formview control. However, you can change the way that the gridview works. Fredrik Normen has built a sample that shows how to overcome that kind of situation:

    http://fredrik.nsquared2.com/viewpost.aspx?PostID=155

    I've also buit another option based on code (if you search the forums you'll find a post which has a link to it).
    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Smile [:)] Re: Gridview Insert Command

    06-01-2005, 11:58 AM
    • Member
      180 point Member
    • mauricioaglr
    • Member since 05-11-2005, 5:12 AM
    • Los Angeles
    • Posts 36
    Thanks.
    Mauricio

    "Whatsoever ye would that men do unto you, do you even so unto them" (Mathew 7:12)
  • Indifferent [:|] Re: Gridview Insert Command

    08-24-2005, 7:54 PM
    • Member
      5 point Member
    • laosong8
    • Member since 08-24-2005, 11:46 PM
    • Posts 1
    You can use Insert Command
    1. create a database and table Customers with 4 colums [Customers] ([CustomerID], [CompanyName], [ContactTitle]) , and insert some records to the table
    2. create connect string "ConnString"
    3. create a aspx file and copy paste the code to your file

    <%@ Page Language="C#" ClassName="Default_aspx" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

    <script runat="server">

    void Button1_Click(object sender, EventArgs e)

    {

    TextBox customerID = GridView1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;

    TextBox companyName = GridView1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;

    DropDownList ContactTitle = GridView1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList;

    SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;

    SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;

    SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue = ContactTitle.SelectedValue;

    SqlDataSource1.Insert();

    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head id="Head1" runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" Runat="server" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID"

    AutoGenerateColumns="False" ShowFooter="True" >

    <Columns>

    <asp:TemplateField>

    <ItemTemplate>

    <asp:Label ID="CustomerIDLabel" Runat="Server"><%# Eval("CustomerID") %></asp:Label>

    </ItemTemplate>

    <FooterTemplate>

    <asp:TextBox ID="CustomerIDTextBox" Runat="server"></asp:TextBox>

    </FooterTemplate>

    </asp:TemplateField>

    <asp:TemplateField>

    <ItemTemplate>

    <asp:Label ID="CompanyNameLabel" Runat="Server"><%# Eval("CompanyName") %></asp:Label>

    </ItemTemplate>

    <FooterTemplate>

    <asp:TextBox ID="CompanyNameTextBox" Runat="server"></asp:TextBox>

    </FooterTemplate>

    </asp:TemplateField>

    <asp:TemplateField>

    <FooterTemplate>

    <asp:DropDownList ID="ContactTitleDropDownList" Runat="server" DataSourceID="SqlDataSource2" DataTextField="ContactTitle" DataValueField="ContactTitle">

    </asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"

    ConnectionString="<%$ ConnectionStrings:ConnString%>">

    </asp:SqlDataSource>

    <asp:Button ID="Button1" Runat="server" Text="Add" OnClick="Button1_Click" />

    </FooterTemplate>

    <ItemTemplate>

    <asp:DropDownList ID="ContactTitleDropDown" SelectedValue='<%# Bind("ContactTitle") %>' Runat="Server" DataSourceID="SqlDataSource3" DataTextField="ContactTitle" DataValueField="ContactTitle" ></asp:DropDownList>

    <asp:SqlDataSource ID="SqlDataSource3" Runat="server" SelectCommand="SELECT DISTINCT [ContactTitle] FROM [Customers]"

    ConnectionString="<%$ ConnectionStrings:ConnString%>" EnableCaching="True">

    </asp:SqlDataSource>

    </ItemTemplate>

    </asp:TemplateField>

    </Columns>

    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" Runat="server"

    InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactTitle]) VALUES (@CustomerID, @CompanyName, @ContactTitle)"

    SelectCommand="SELECT [CustomerID], [CompanyName], [ContactTitle] FROM [Customers]"

    ConnectionString="<%$ ConnectionStrings:ConnString%>">

    <DeleteParameters>

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

    </DeleteParameters>

    <UpdateParameters>

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

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

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

    </UpdateParameters>

    <InsertParameters>

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

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

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

    </InsertParameters>

    </asp:SqlDataSource>

    </div>

    </form>

    </body>

    </html>

  • Re: Gridview Insert Command

    05-29-2008, 2:39 AM
    • Member
      4 point Member
    • MrDOOM
    • Member since 05-29-2008, 6:36 AM
    • Posts 2
    laosong8:

    void Button1_Click(object sender, EventArgs e)

    {

    TextBox customerID = GridView1.FooterRow.FindControl("CustomerIDTextBox") as TextBox;

    TextBox companyName = GridView1.FooterRow.FindControl("CompanyNameTextBox") as TextBox;

    DropDownList ContactTitle = GridView1.FooterRow.FindControl("ContactTitleDropDownList") as DropDownList;

     

    SqlDataSource1.InsertParameters["CustomerID"].DefaultValue = customerID.Text;

    SqlDataSource1.InsertParameters["CompanyName"].DefaultValue = companyName.Text;

    SqlDataSource1.InsertParameters["ContactTitle"].DefaultValue = ContactTitle.SelectedValue;

     

    SqlDataSource1.Insert();

    }

    THANK U laosong8

    Many thanks WinkParty!!!
  • Re: Gridview Insert Command

    08-18-2008, 4:51 AM
    • Member
      4 point Member
    • McFry
    • Member since 08-18-2008, 8:46 AM
    • Posts 2

    Hi,

    There is a good article here about this as well:

    http://www.codeproject.com/KB/webforms/addupdate.aspx

    Cheers,

    McFry

  • Re: Gridview Insert Command

    02-13-2009, 9:07 PM
    • Member
      2 point Member
    • GordW
    • Member since 01-27-2009, 12:58 AM
    • Posts 4

    I don't understand why anybody would want to go to the trouble of emulating an Insert function in a GridView when they can simply add a FormView somewhere on the page, set the DefaultMode to Insert, and provide only an Insert Template.

  • Re: Gridview Insert Command

    05-15-2009, 8:40 AM
    • Member
      24 point Member
    • Anto.NET
    • Member since 11-12-2005, 3:08 PM
    • Posts 6

    because sometimes (always?) an easy-to-understand user interface is critical to the client acceptance of a new application. And all the details are relevant.

    IMHO.

Page 1 of 1 (8 items)