AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 asp.net gridview update, delete manualy
Posted by Sanjeev Gupta on  1/24/2008 2:53:38 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
i am using asp.net2.0 and vs2005. i have a problam when updating in gridview after update update and cancel button reside not change in edit button.i want to change edit button
//////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Forms_product : System.Web.UI.Page
{
productClass pc = new productClass();
dbConnection dbcon = new dbConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGrid();

}
void BindGrid()
{
DataSet ds = new DataSet();
string connstring;
connstring = dbcon.ConString;
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = connstring;
myConnection.Open();
SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM dbo.tblProducts", myConnection);
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
private int index = 1;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblref = (Label)e.Row.Cells[1].FindControl("lblrefNo");
lblref.Text = index.ToString();
index = index + 1;
}
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

int ID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox name = (TextBox)row.FindControl("TextBox1");
TextBox desc = (TextBox)row.FindControl("TextBox2");
Boolean authenticated;
Boolean returnvalue;
authenticated = pc.productcheck(name.Text);
returnvalue = authenticated;
if (returnvalue)
{
pc.productupdate(desc.Text, ID);

BindGrid();

ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Product Already Exist But Description updated ');</script>");


}
else
{
pc.productupdate(name.Text,desc.Text, ID);


BindGrid();
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Data Updated');</script>");

}
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Data Updated');</script>");
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();

}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
pc.productdelete(ID);
BindGrid();
ClientScript.RegisterStartupScript(GetType(), "Message", "<SCRIPT LANGUAGE='javascript'>alert('Data Deleted');</script>");


}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();

}


protected void btnsubmit_Click(object sender, EventArgs e)
{
Response.Redirect("productInsert.aspx");
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
}

Post Your Reply Post New Question

 Viewer's Reply
Set EditIndex as -1
Answered By Moderator1 on 1/28/2008 7:15:04 PM
Hi,

In your GridView's RowUpdating, after you perform update operation and before you call BindGrid method you have to set

GridView1.EditIndex=-1
BindGrid()

It will change the update mode to edit mode.
Thanks
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Everything You Know About CSS Is Wrong! - Free 37 Page Preview!
The PHP Anthology: 101 Essential Tips, Tricks & Hacks, 2nd Edition - Free 207 Page Preview!
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview