now, i will explain how to data bind in formview and update,delete,cancle event .
here,aspx page
here,aspx page
<asp:FormView ID="fv" runat="server" AllowPaging="True" OnPageIndexChanging="fv_PageIndexChanging">
<HeaderTemplate>
<table>
<tr>
<td width="100px">
Id:
</td>
<td width="100px">
Name:
</td>
<td width="100px">
Marks:
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td width="100px">
<asp:Label ID="lblid" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
</td>
<td width="100px">
<asp:Label ID="lblname" runat="server" Text='<%#Eval("sname") %>'></asp:Label>
</td>
<td width="100px">
<asp:Label ID="lblmarks" runat="server" Text='<%#Eval("marks") %>'></asp:Label>
</td>
</tr>
</table>
<asp:LinkButton ID="btnedit" runat="server" OnClick="btnedit_Click">Edit</asp:LinkButton>
<asp:LinkButton ID="btninsert" runat="server" onclick="btninsert_Click">Insert</asp:LinkButton>
<asp:LinkButton ID="btndelete" runat="server" onclick="btndelete_Click">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td width="100px">
<asp:Label ID="lblid" runat="server" Text='<%#Eval("sid")%>'></asp:Label>
</td>
<td width="100px">
<asp:TextBox ID="txtname" runat="server" Text='<%#Bind("sname") %>'></asp:TextBox>
</td>
<td width="100px">
<asp:TextBox ID="txtmarks" runat="server" Text='<%#Bind("marks") %>'></asp:TextBox>
</td>
</tr>
</table>
<br>
</br>
<asp:LinkButton ID="btnupdate" runat="server" OnClick="btnupdate_Click">Update</asp:LinkButton>
<asp:LinkButton ID="btncancle" runat="server" OnClick="btncancle_Click">Cancle</asp:LinkButton>
</EditItemTemplate>
</asp:FormView>
After that write the following code aspx.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default :
System.Web.UI.Page
{
static SqlConnection
con = new SqlConnection("Data Source -PC\\SQLEXPRESS;Initial Catalog;Integrated
Security=True;Pooling=False");
static SqlDataAdapter
da = new SqlDataAdapter("select *from student",con);
SqlCommandBuilder cmdb = new
SqlCommandBuilder(da);
DataTable dt = new
DataTable();
protected void
Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
da.Fill(dt);
ViewState["dt"] = dt;
bind();
}
else
{
dt = (DataTable)ViewState["dt"];
}
}
protected void bind()
{
fv.DataSource = dt;
fv.DataBind();
}
protected void
fv_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
fv.PageIndex = e.NewPageIndex;
bind();
}
protected void
btnedit_Click(object sender, EventArgs e)
{
fv.ChangeMode(FormViewMode.Edit);
bind();
}
protected void
btnupdate_Click(object sender, EventArgs e)
{
DataRow[] dr;
dr =
dt.Select("sid="+((Label)fv.FindControl("lblid")).Text);
dr[0][1] = ((TextBox)fv.FindControl("txtname")).Text;
dr[0][2] = ((TextBox)fv.FindControl("txtmarks")).Text;
da.Update(dt);
fv.ChangeMode(FormViewMode.ReadOnly);
bind();
}
protected void
btncancle_Click(object sender, EventArgs e)
{
fv.ChangeMode(FormViewMode.ReadOnly);
bind();
}
protected void
btndelete_Click(object sender, EventArgs e)
{
DataRow[] dr;
dr =
dt.Select("sid="+((Label)fv.FindControl("lblid")).Text);
dr[0].Delete();
da.Update(dt);
bind();
}
protected void
btninsert_Click(object sender, EventArgs e)
{
int max=0;
for (int i = 0; i
< fv.PageCount; i++)
{
fv.PageIndex = i;
bind();
int h =Convert.ToInt32(((Label)fv.FindControl("lblid")).Text);
if(max<h)
{
max = h;
}
}
DataRow dr;
dr =
dt.Rows.Add();
fv.PageIndex = fv.PageCount;
bind();
dr[0]
= max + 1;
fv.ChangeMode(FormViewMode.Edit);
bind();
da.Update(dt);
}
}