edit, update, delete, cancel, insert within Gridview.....(veryuseful for beginners)

Ankitsrist
Posted by Ankitsrist under ASP.NET category on | Points: 40 | Views : 1798
hi, this is the complete code for edit, update, delete, cancel, insert within Gridview by using template field......
asp.cs page
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
dbClass dbc = new dbClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
fillgrid();

}
}
public void fillgrid()
{
dbc.myconnection();
SqlCommand cmd = new SqlCommand("select * from dmlwithingrid", dbc .sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
adp.Fill(ds, "table1");
GridView1.DataSource = ds;
GridView1.DataBind();

}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
dbc.myconnection();
GridView1.EditIndex = e.NewEditIndex;
fillgrid();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dbc.myconnection();
GridView1.EditIndex = -1;
fillgrid();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
dbc.myconnection();
if (e.CommandName.Equals("insert"))
{
TextBox txt1 = (TextBox)GridView1.FooterRow.FindControl("txtn");
TextBox txt2 = (TextBox)GridView1.FooterRow.FindControl("txtd");
SqlCommand cmd = new SqlCommand("insert into dmlwithingrid(name, decription)values('"+txt1.Text +"', '"+txt2.Text +"')", dbc .sqlcon );
cmd.ExecuteNonQuery();
fillgrid();

}

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
dbc.myconnection();
int id = Convert.ToInt32(GridView1 .DataKeys [e.RowIndex ].Values["id"].ToString ());
SqlCommand cmd = new SqlCommand("delete from dmlwithingrid where id="+id, dbc.sqlcon );
cmd.ExecuteNonQuery();
fillgrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dbc.myconnection();
int id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex ].Value.ToString());
string name = GridView1.DataKeys[e.RowIndex].Values["name"].ToString();
TextBox txtcity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtcity");
SqlCommand cmd = new SqlCommand("update dmlwithingrid set decription='" + txtcity.Text + "' where id=" + id, dbc.sqlcon);
cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
fillgrid();
}
}

----------.aspx page---------
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div> <asp:GridView ID="GridView1" DataKeyNames="id, name" runat="server"
AutoGenerateColumns="false" CssClass="Gridview" HeaderStyle-BackColor="#61A6F8"
ShowFooter="true" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowcommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Height="184px" Width="489px">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="imgbtnUpdate" CommandName="Update" runat="server" Text ="update"/>
<asp:LinkButton ID="imgbtnCancel" runat="server" CommandName="Cancel" Text ="cancel"/>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="imgbtnEdit" CommandName="Edit" runat="server" Text="edit" />
<asp:LinkButton ID="imgbtnDelete" CommandName="Delete" Text="delete" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="imgbtnAdd" runat="server" CommandName="insert" Text ="insert"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="name">
<EditItemTemplate>
<asp:Label ID="lbleditusr" runat="server" Text='<%#Eval("name") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%#Eval("name") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtn" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="description">
<EditItemTemplate>
<asp:TextBox ID="txtcity" runat="server" Text='<%#Eval("decription") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblcity" runat="server" Text='<%#Eval("decription") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtd" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#61A6F8" Font-Bold="True" ForeColor="White" />
</asp:GridView>
&nbsp;&nbsp;<br />
&nbsp;

</div>
</asp:Content>

happy coding :)

Comments or Responses

Login to post response