I am using this code for row update of the gridview. But when i update the specific record of the row say 3 it is still giving the exception that "There is no row at position 3. Help me how can i update row of the gridview.?
Here i am posting the code which i am using.

public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=TSARavi\\SQL2005,1435;Initial Catalog=TestDB;User ID=testdb;Password=testdb1"); DataSet ds; CompanyDetail cd; SqlDataAdapter da; DataTable taskTable; protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { taskTable = new DataTable("CompanyDetails"); // Create the columns. //taskTable.Columns.Add("CompanyName", typeof(string)); //taskTable.Columns.Add("ContactName", typeof(string)); //taskTable.Columns.Add("ContactTitle", typeof(string)); //taskTable.Columns.Add("Country", typeof(string)); //taskTable.Columns.Add("Phone", typeof(int)); //taskTable.Columns.Add("Address", typeof(string)); //taskTable.Columns.Add("City", typeof(string));
//Persist the table in the Session object. Session["TaskTable"] = taskTable; //Bind data to the GridView control. BindData(); } } protected void BindData() { con.Open(); string qry = "Select * from CompanyDetails"; ds = new DataSet(); da = new SqlDataAdapter(qry, con); da.Fill(ds); grdCountry.DataSource = ds; grdCountry.DataBind(); con.Close(); } protected void EditRecord(object sender, GridViewEditEventArgs e) { grdCountry.EditIndex = e.NewEditIndex; BindData(); } protected void CancelRecord(object sender, GridViewCancelEditEventArgs e) { grdCountry.EditIndex = -1; BindData(); } protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) { DataTable dt = (DataTable)Session["TaskTable"]; GridViewRow row = grdCountry.Rows[e.RowIndex]; dt.Rows[row.DataItemIndex]["CompanyName"] = ((TextBox)row.Cells[1].Controls[0]).Text; dt.Rows[row.DataItemIndex]["ContactName"] = ((TextBox)row.Cells[2].Controls[0]).Text; dt.Rows[row.DataItemIndex]["ContactTitle"] = ((TextBox)(row.Cells[3].Controls[0])).Text; dt.Rows[row.DataItemIndex]["Country"] = ((TextBox)(row.Cells[4].Controls[0])).Text; dt.Rows[row.DataItemIndex]["Phone"] = ((TextBox)(row.Cells[5].Controls[0])).Text; dt.Rows[row.DataItemIndex]["Address"] = ((TextBox)(row.Cells[6].Controls[0])).Text; dt.Rows[row.DataItemIndex]["City"] = ((TextBox)(row.Cells[7].Controls[0])).Text; grdCountry.DataSource = dt; grdCountry.DataBind(); } protected void DeleteRecord(object sender, GridViewDeleteEventArgs e) { string autoid = grdCountry.DataKeys[e.RowIndex].Value.ToString(); } }