Hello, I have a gridview bind to data table. But gridview can't add row directly. So what is the best way to add new row. I used to drag several text box on the web form. By filling the text into text boxs and using sql to write the data to the database
and bind gridview again.
I do think that it is a stupid way. Can we click an "ADD" button and bring up a detailview something like to add new record?
I used to drag several text box on the web form. By filling the text into text boxs and using sql to write the data to the database and bind gridview again.
That's generally how I add rows into my GridViews.
zhshqzyc
I do think that it is a stupid way.
You're probably right in implying that my method is a bit goofy, but it is easy to implement.
zhshqzyc
Can we click an "ADD" button and bring up a detailview something like to add new record?
I think so but I have never done it personally. I'm pretty sure the DetailsView allows insertion of data:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx
Another option might be to just add a dummy row into the GridView when the user clicks your insert button and immediately mark the newly added row for editing. Then they user should be able to use the EditItemTemplate to modify the dummy row to the values
desired.
I tried your code, but it doesn't display the footers unless something is in the dataset. Is there a way to display the footer when there is nothing in the dataset?
zhshqzyc
Member
416 Points
517 Posts
Insert a new row to gridview
May 06, 2012 02:34 AM|LINK
Hello, I have a gridview bind to data table. But gridview can't add row directly. So what is the best way to add new row. I used to drag several text box on the web form. By filling the text into text boxs and using sql to write the data to the database and bind gridview again.
I do think that it is a stupid way. Can we click an "ADD" button and bring up a detailview something like to add new record?
Need your advice.
Many thanks.
hoopslife
Contributor
2477 Points
483 Posts
Re: Insert a new row to gridview
May 06, 2012 03:11 AM|LINK
Using the DetailsView is a good way to go. Check out this video tutorial using the DetailsView with the GridView:
http://www.youtube.com/watch?v=ZzZg2-MAEAo
Blog
Shawn Eary
Member
275 Points
52 Posts
Re: Insert a new row to gridview
May 06, 2012 03:27 AM|LINK
That's generally how I add rows into my GridViews.
You're probably right in implying that my method is a bit goofy, but it is easy to implement.
I think so but I have never done it personally. I'm pretty sure the DetailsView allows insertion of data:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.aspx
Another option might be to just add a dummy row into the GridView when the user clicks your insert button and immediately mark the newly added row for editing. Then they user should be able to use the EditItemTemplate to modify the dummy row to the values desired.
Also, the ListView seems to have native support for inserting records:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.aspx
ZeeshanAnsar...
Participant
878 Points
264 Posts
Re: Insert a new row to gridview
May 06, 2012 03:27 AM|LINK
see this article
http://aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx
Please 'Mark as Answer' if this post helps you.
basheerkal
Star
10672 Points
2426 Posts
Re: Insert a new row to gridview
May 06, 2012 06:52 AM|LINK
Best thing is to use a combination of a FormView and GridView
You can insert using default functionality of formView and see the updated data in gridview without writing any code in cod ebehind.
Or you can use this sample aspx page.
In this texboxes are added to footer of the gridView for user to type in values and using the Rowcommand items are inserted in DB.
<%@ Page Language="VB" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Dim _Description As String = DirectCast(GridView1.FooterRow.FindControl("txtDescription"), TextBox).Text Dim _Mother As String = DirectCast(GridView1.FooterRow.FindControl("txtMother"), TextBox).Text Dim _Child As String = DirectCast(GridView1.FooterRow.FindControl("txtChild"), TextBox).Text If e.CommandName.Equals("Insert") Then SD.InsertParameters("Father").DefaultValue = _Description SD.InsertParameters("Mother").DefaultValue = _Mother SD.InsertParameters("Child").DefaultValue = _Child SD.Insert() End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SD" ShowFooter="True" AllowPaging="True" onrowcommand="GridView1_RowCommand" EnableTheming="False" > <Columns> <asp:TemplateField ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LBEdit" runat="server" CommandName ="edit">Edit</asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="LbUpdate" runat="server" CommandName ="update">Update</asp:LinkButton> <asp:LinkButton ID="Lbcancel" runat="server" CommandName ="Cancel">Cancel</asp:LinkButton> </EditItemTemplate> <FooterTemplate> <asp:LinkButton ID="lbInsert" runat="server" CommandName="Insert">Insert</asp:LinkButton> </FooterTemplate> </asp:TemplateField> <asp:BoundField DataField="Id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" /> <asp:TemplateField HeaderText="father" SortExpression="Father"> <EditItemTemplate> <asp:TextBox ID="txtDescripton" runat="server" Text='<%# Bind("Father") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Father") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtDescription" runat="server" /></FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mother" SortExpression="Mother"> <EditItemTemplate> <asp:TextBox ID="txtMother" runat="server" Text='<%# Bind("Mother") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblMother" runat="server" Text='<%# Bind("Mother") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtMother" runat="server" /></FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Child" SortExpression="Child"> <EditItemTemplate> <asp:TextBox ID="txtChild" runat="server" Text='<%# Bind("Child") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblChild" runat="server" Text='<%# Bind("Child") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="txtChild" runat="server" /></FooterTemplate> </asp:TemplateField> </Columns> <HeaderStyle BorderStyle="None" /> </asp:GridView> <asp:SqlDataSource ID="SD" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnection %>" SelectCommand="SELECT * FROM [Table2] ORDER BY ID DESC" InsertCommand="INSERT INTO [table2] ([Father], [Mother], [Child]) VALUES (@Father, @Mother, @Child)" UpdateCommand="UPDATE [Table2] SET [Father] = @father, [Mother] = @Mother, [Child] = @Child WHERE [Id] = @Id" > <InsertParameters> <asp:Parameter Name="father" Type="String" /> <asp:Parameter Name="Mother" Type="String" /> <asp:Parameter Name="Child" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="father" Type="String" /> <asp:Parameter Name="Mother" Type="String" /> <asp:Parameter Name="Child" Type="String" /> <asp:Parameter Name="Id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </div> </form> </body> </html>VB.net is used in code behind. You may convert it by visiting http://www.developerfusion.com/tools/convert/vb-to-csharp/
(Talk less..Work more)
liefie2000
Member
16 Points
81 Posts
Re: Insert a new row to gridview
Feb 25, 2013 04:23 PM|LINK
I tried your code, but it doesn't display the footers unless something is in the dataset. Is there a way to display the footer when there is nothing in the dataset?