C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Add New Row to GridView and save it to Database


Posted Date: 06 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: sriluMember Level: Diamond    
Rating: Points: 10



This code sample shows how to add a new blank row to GridView and insert those values to the database.

Gridview example can look like:


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" ShowFooter="True">
<Columns>
<asp:TemplateField HeaderText="empid">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("empid") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="footertxtid" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("empid") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("empname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="footertxtname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("empname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Add"
Text="Add"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="footerlnksave" runat="server" Text="Save" CommandName="Save"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



In the row command of the GridView, the code could be


if (e.CommandName == "Add")
{
GridView1.FooterRow.Visible = true;
}
else if(e.CommandName=="Save")
{
TextBox txtid = (TextBox)GridView1.FooterRow.FindControl("footertxtid");
int id = Convert.ToInt32(txtid.Text);
TextBox txtname = (TextBox)GridView1.FooterRow.FindControl("footertxtname");
string name = Convert.ToString(txtname.Text);
}
SqlCommand cmd = new SqlCommand("insert into emp values(id,name)",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
bindgrid();//in bindgrid write GridView1.FooterRow.Visible =false;
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Save from gridview to database  .  Add new row to gridview  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Play Flash Files in ASP.NET
Previous Resource: using log4net.dll in our applications
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use