Take RowCommand command Property of data grid view. and write bellow code in code behind when you click on view it go to row command and check for command name here i have command name Edit Record for update I assign the selected row value to textboxes you can assign that values to label also.
protected void grdvwParts_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int id = Convert.ToInt32(e.CommandArgument);
if (e.CommandName.Equals("EditRecord")) //onclick edit link button of grigview
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
string partCode = row.Cells[1].Text;
string partname = row.Cells[2].Text;
string section = row.Cells[3].Text;
tbcPrtCode.Text = partCode;
tbxPrtNm.Text = partname;
ddlsection.SelectedItem.Text = section;
}
if (e.CommandName.Equals("DeleteRecord")) //onclick remove link button of grigview
{
// ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert(' Are you sure')", true);
con = new SqlConnection(strConnString);
cmd = new SqlCommand(String.Format("DELETE FROM Parts WHERE(prt_id ='" + id + "')"), con);
cmd.Connection = con;
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (con != null)
{
con.Close();
con.Dispose();
}
}
hope it will help you