C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Gridview with edit,delete operations


Posted Date: 21 Jul 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: mahanteshMember Level: Gold    
Rating: 1 out of 5Points: 7



Description :


This code shows basic gridview operations like Edit, Delete etc. in Asp.net

//HTML code

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
PageSize="3" onpageindexchanging="GridView1_PageIndexChanging">
<Columns>


<asp:TemplateField HeaderText="EmployeeNo">
<ItemTemplate>
<%#Eval("EmployeeNo")%>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtEmployeeNo" runat="server" Text=' <%#Eval("EmployeeNo")%> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtName" runat="server" Text=' <%#Eval("Name")%> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<%#Eval("Email")%>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox ID="txtEmail" runat="server" Text=' <%#Eval("Email")%> '> </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>


</Columns>
</asp:GridView>


//int the .cs file
//Gridview with edit,delete operations


public partial class GridOperations : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
//Bind the GridView from database
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//point to current index
GridView1.EditIndex = e.NewEditIndex;
//Bind the GrisView from database
bind();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
//Bind the GridView from database
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
//Row delete
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox email = (TextBox)row.FindControl("txtEmail");
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("delete from tab1 where Email='" + email.Text + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();

}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
TextBox name= (TextBox)row.FindControl("txtName");
TextBox empno = (TextBox)row.FindControl("txtEmployeeNo");
TextBox email = (TextBox)row.FindControl("txtEmail");
GridView1.EditIndex = -1;
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("update tab1 set Name='" + name.Text + " ',Email='" + email.Text + "' where EmployeeNo='" + empno.Text + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
bind();

}
//Bind the GrisView from database

public void bind()
{
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select EmployeeNo,Name,Email from tab1", conn);
DataSet ds = new DataSet();
da.Fill(ds, "emp");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
conn.Close();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bind();
}
}



Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Gridview with edit  .  Delete operations  .  

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: GridView with template columns
Previous Resource: Validating Textbox Inside a GridView and to Restrict the similar Text.
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use