Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Insert new data into gridview
Just like we do in windows application to insert new data into table
from datagridview.


please help me.
thanks in advance
Posted
Updated 28-Dec-11 21:13pm
v2
Comments
nagendrathecoder 29-Dec-11 2:03am    
How are you inserting data from gridview, please show us some code.
Ankur\m/ 29-Dec-11 3:58am    
There are lots of tutorials for this on the web. Do a Google search, please.

The GridView does not inherently support insertion of records, but you can accomplish it by creating a FooterTemplate in which you create the fields for entry of new data. Read Complete Answer[^] of Similar how to insert row in grid view without using SQLDataSource[^]
 
Share this answer
 
 create a three column Table
	    Dim Table1 As Table = New Table
for i=1 to x ' loop throught x times to creat x many rows of three columns each
            Dim tr As New TableRow
            Dim ta, tb, tc As New TableCel
            ta.Text = "Text for Cell1"
            ta.Width = 60
            tr.Cells.Add(ta) 'Adds cell1 to the row
            tb.Text = "Text for Cell2
            tb.Width = 120
            tr.Cells.Add(tb) 'Adds cell2 to the row

            tc.Text = "Text for cell3"
            tc.HorizontalAlign = HorizontalAlign.Left
            tc.Width = 300
            tc.VerticalAlign = VerticalAlign.Top
            tr.Cells.Add(tc)  'Adds cell2 to the row
            tr.BorderStyle = BorderStyle.Solid
            tr.BorderWidth = 4
            Table1.Rows.Add(tr) 'adds the rows to the table
next
 
Share this answer
 
v3
This is i got the solution: to insert new row if no data is their:


<asp:GridView ID="Customers" runat="server" AutoGenerateColumns="False"
        DataSourceID="CustomersSqlDataSource" DataKeyNames="CustomerId" ShowFooter="true"
        onrowcommand="Customers_RowCommand">
        <Columns>
            <asp:TemplateField HeaderText="CustomerId">
                <ItemTemplate>
                    <%# Eval("CustomerId") %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FirstName">
                <ItemTemplate>
                    <%# Eval("FirstName") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="EditFirstName" Text=''
                    <%# Bind("FirstName") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox runat="server" ID="InsertFirstName" Text=''
                    <%# Bind("FirstName") %>' />
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="LastName">
                <ItemTemplate>
                    <%# Eval("LastName") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="EditLastName" Text=''
                    <%# Bind("LastName") %>' />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox runat="server" ID="InsertLastName" Text=''
                    <%# Bind("LastName") %>' />
                </FooterTemplate>
            </asp:TemplateField>
 
            <asp:TemplateField HeaderText="Commands">
                <ItemTemplate>
                    <asp:Button runat="server" ID="Edit" Text="Edit" CommandName="Edit" />
                    <asp:Button runat="server" ID="Delete" Text="Delete" CommandName="Delete" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Button runat="server" ID="Update" Text="Update" CommandName="Update" />
                    <asp:Button runat="server" ID="Cancel" Text="Cancel" CommandName="Cancel" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:Button runat="server" ID="Insert" Text="Insert" CommandName="InsertNew" />
                    <asp:Button runat="server" ID="Cancel" Text="Cancel" CommandName="CancelNew" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <EmptyDataTemplate>
            First Name:<asp:TextBox runat="server" ID="NoDataFirstName" />
            Last Name:<asp:TextBox runat="server" ID="NoDataLastName" />
            <asp:Button runat="server" ID="NoDataInsert" CommandName="NoDataInsert" Text="Insert"/>
        </EmptyDataTemplate>
    </asp:GridView>
 
Share this answer
 
v2
Comments
RaviRanjanKr 30-Dec-11 3:04am    
[Edited]Code is wrapped in pre tag[/Edited]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900