gridview add new row

In this post we will see how to add new row to grid. Gridview have a footer template. By default the visibility is false for this footer template . On add new row button click event, we will set the visibility of Gridview footer row to true and bind the grid again.

Aspx code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> 
<Columns> 
<asp:TemplateField HeaderText="ID"> 
<ItemTemplate> 
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'> 
</ItemTemplate> 
<FooterTemplate> 
 
</FooterTemplate> 

</asp:TemplateField> 
<asp:TemplateField HeaderText="First Name"> 
<ItemTemplate> 
<asp:Label ID="lblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtFirstName" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Last Name"> 
<ItemTemplate> 
<asp:Label ID="lblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtLastName" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Email"> 
<ItemTemplate> 
<asp:Label ID="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label> 
</ItemTemplate> 
<FooterTemplate> 
<asp:TextBox ID="txtEmail" runat="server"> 
</FooterTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Check all"> 
<HeaderTemplate> 
<asp:CheckBox ID="CheckBox2" AutoPostBack="true" runat="server"  OnCheckedChanged="CheckBox2_CheckedChanged1" /> 
</HeaderTemplate> 
<ItemTemplate> 
<asp:CheckBox ID="CheckBox1"  runat="server" /> 
</ItemTemplate> 
<FooterTemplate> 
<asp:LinkButton ID="lnkSave" runat="server" CommandName="Save" OnClick="lnkSave_Click">Save 
</FooterTemplate> 
</asp:TemplateField> 
</Columns> 
</asp:GridView>



C# code behind
using System; 
using System.Data; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
BindGrid(); 
} 
} 
private void BindGrid() 
{ 
GridView1.DataSource = GridDataProvider.GetData(); 
GridView1.DataBind(); 
} 

protected void CheckBox2_CheckedChanged1(object sender, EventArgs e) 
{ 
foreach (GridViewRow r in GridView1.Rows) 
{ 
((CheckBox)r.FindControl("CheckBox1")).Checked = true; 
} 
} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
} 
protected void Button2_Click(object sender, EventArgs e) 
{ 
GridView1.ShowFooter = true; 
BindGrid(); 
} 
protected void lnkSave_Click(object sender, EventArgs e) 
{ 
string ID = (  (TextBox)  GridView1.FooterRow.FindControl("txtID") ).Text ; 
string firstName = (  (TextBox)  GridView1.FooterRow.FindControl("txtFirstName") ).Text; 
// similarly you can find other controls and save 
} 





Vb.Net Code

Imports System 
Imports System.Data 
Imports System.Configuration 
Imports System.Web 
Imports System.Web.Security 
Imports System.Web.UI 
Imports System.Web.UI.WebControls 
Imports System.Web.UI.WebControls.WebParts 
Imports System.Web.UI.HtmlControls 
Public Partial Class _Default 
Inherits System.Web.UI.Page 
Protected Sub Page_Load(sender As Object, e As EventArgs) 
If Not Page.IsPostBack Then 
BindGrid() 
End If 
End Sub 
Private Sub BindGrid() 
GridView1.DataSource = GridDataProvider.GetData() 
GridView1.DataBind() 
End Sub 

Protected Sub CheckBox2_CheckedChanged1(sender As Object, e As EventArgs) 
For Each r As GridViewRow In GridView1.Rows 
(DirectCast(r.FindControl("CheckBox1"), CheckBox)).Checked = True 
Next 
End Sub 
Protected Sub Button1_Click(sender As Object, e As EventArgs) 
End Sub 
Protected Sub Button2_Click(sender As Object, e As EventArgs) 
GridView1.ShowFooter = True 
BindGrid() 
End Sub 
Protected Sub lnkSave_Click(sender As Object, e As EventArgs) 
Dim ID As String = (DirectCast(GridView1.FooterRow.FindControl("txtID"), TextBox)).Text 
Dim firstName As String = (DirectCast(GridView1.FooterRow.FindControl("txtFirstName"), TextBox)).Text 
' similarly you can find other controls and save 
End Sub 
End Class 

0 Comments :

Post a Comment