Hi,See this.Here when you click edit Update and cancel links will come. This updateRow is your requirement. i.e, Populate grid vlues to twxt box
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="false" OnRowEditing="EditRow" OnRowCancelingEdit="CancelEditRow"
OnRowUpdating="UpdateRow" DataKeyNames="AutoId" OnRowDeleting="DeleteRow" AllowPaging="true"
PageSize="2" OnPageIndexChanging="ChangePage" AllowSorting="true" OnSorting="SortRecords">
Code behind:
Page Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateData();
}
}
PopulateData:
private void PopulateData()
{
DataTable table = new DataTable();
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Select * from PersonDetails";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
ad.Fill(table);
}
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
Edit Row:
protected void EditRow(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.PopulateData();
}
CanceEditRow:
protected void CancelEditRow(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
this.PopulateData();
}
DEleteRow:
protected void DeleteRow(object sender, GridViewDeleteEventArgs e)
{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Delete from PersonDetails" + " where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@AutoId", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record has been deleted successfully !";
lblMessage.ForeColor = System.Drawing.Color.Red;
this.PopulateData();
}
UpdateRow:
protected void UpdateRow(object sendedr, GridViewUpdateEventArgs e)
{
var autoID = GridView1.DataKeys[e.RowIndex].Value;
GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
TextBox tFirstName = row.FindControl("txtFirstName") as TextBox;
TextBox tLastName = row.FindControl("txtLastName") as TextBox;
TextBox tAge = row.FindControl("txtAge") as TextBox;
DropDownList dropActive = row.FindControl("dropActive") as DropDownList;
using (SqlConnection conn = new SqlConnection(_connStr))
{
string sql = "Update PersonDetails set FirstName = @FirstName, LastName=@LastName, Age= @Age, Active = @Active" +
" where AutoId = @AutoId";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@FirstName", tFirstName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", tLastName.Text.Trim());
cmd.Parameters.AddWithValue("@Age", tAge.Text.Trim());
cmd.Parameters.AddWithValue("@Active", dropActive.SelectedValue);
cmd.Parameters.AddWithValue("@AutoId", autoID);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
lblMessage.Text = "Record updated successfully !";
GridView1.EditIndex = -1;
this.PopulateData();
}
Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator