Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my gridview look like this:

http://s21.postimg.org/q66a19s5j/image.jpg

when click the edit button it pass value into above textbox(i show in my grid image)...

After i click modify button gridview showing update value in gridvie:

After editing gridview will be like this:

http://s24.postimg.org/i09kj3tyd/image.jpg[^]


i add my edit and delete as linkbutton using templatefield:

I used following code for this editing process:


    public partial class Designation : System.Web.UI.Page
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter ad;
        DataTable dt;
        DataTable dtDesign;
        DataSet ds;
        string qry;
        Sequence objSeq;
        protected void Page_Load(object sender, EventArgs e)
        {

            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Ondemand"].ConnectionString);
            display();
            GetID();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
        }
        protected void display()
        {
            try
            {

                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Open();
                ad = new SqlDataAdapter("select designation_name,designation_id from designation where type='N' order by designation_name asc", conn);
                dtDesign = new System.Data.DataTable();
                ad.Fill(dtDesign);
                if (dtDesign.Rows.Count > 0)
                {
                    DataView dv = new DataView(dtDesign);
                    grid_viewDesig.DataSource = dv;
                    grid_viewDesig.DataBind();


                }
                else
                {
                    grid_viewDesig.Columns.Clear();
                    //btnNew_Click(null, null);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
            }
            finally
            {
                conn.Close();
                //grid_viewdept.SelectionChanged += new EventHandler(DataGridView1_SelectionChanged);

            }

        }
        protected void pageindexchanging(object s, GridViewPageEventArgs e)
        {
            grid_viewDesig.PageIndex = e.NewPageIndex;
            display();
        }
        protected void grid_rowdelete_Desig(object s, GridViewDeleteEventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                //string id=grid_viewdept.DataMember.
                string id = (string)grid_viewDesig.DataKeys[e.RowIndex].Value;

                conn.Open();
                cmd = new SqlCommand("update designation set type='D' where designation_id='" + id + "'", conn);
                cmd.ExecuteNonQuery();
                LblMessage.Text = "Your record Deleted !";
                display();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
            }
            finally
            {

            }


        }
        protected void grid_rowediting_desig(object s, GridViewEditEventArgs e)
        {

           
        }
        private void Clear()
        {
            txtDesignation_name.Text = "";
            //txtDepartment_id.Text = "";
            LblMessage.Text = "";
            txtDesignation_name.Focus();
        }
        private void GetID()
        {
            try
            {
                objSeq = new Sequence();
                ID = objSeq.GetSequenceID("designation");
                txtDesignation_id.Text = ID;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
                ex = null;
            }
            finally
            {
                objSeq = null;
            }
        }
        private void UpdateID()
        {
            try
            {
                objSeq = new Sequence();
                objSeq.UpdateSequenceID("designation");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
                ex = null;
            }
            finally
            {
                objSeq = null;
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
            GetID();
        }

        protected void btn_addnew_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                conn.Open();
                if (txtDesignation_name.Text != "")
                {
                    ad = new SqlDataAdapter("select * from designation where designation_name='" + txtDesignation_name.Text + "'", conn);
                    dt = new System.Data.DataTable();
                    ad.Fill(dt);
                    if (dt.Rows.Count > 0)
                    {
                        MessageBox.Show("Already this Designation Used!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);
                        //txtDesignation_name.Select();
                        txtDesignation_name.Focus();
                    }
                    else
                    {
                        GetID();

                        cmd = new SqlCommand("insert into designation(designation_id,designation_name,login_id,type,created_on,created_by) values('" + txtDesignation_id.Text
                            + "','" + txtDesignation_name.Text + "','" + Session["name"] + "','N','" + DateTime.Now + "','" + SystemInformation.UserName + "')", conn);
                        cmd.ExecuteNonQuery();
                        UpdateID();
                        display();
                        LblMessage.Text = "Successfull Inserted";
                        txtDesignation_name.Text = "";
                        txtDesignation_name.Focus();
                    }
                }
                else
                {
                    LblMessage.Text = "Pls Insert Designation Name";
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        protected void btnmodify_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();

                //string id=grid_viewdept.DataMember.


                conn.Open();
                cmd = new SqlCommand("update designation set designation_name='" + txtDesignation_name.Text + "' where designation_id='" + txtDesignation_id.Text + "' and type='N'", conn);
                cmd.ExecuteNonQuery();
                LblMessage.Text = "Your record is Update !";
                display();
                btn_addnew.Visible = true;
                btnmodify.Visible = false;
                txtDesignation_name.Text = "";
                txtDesignation_name.Focus();

                GetID();
                //display();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception");
            }
            finally
            {

            }
           
        }

        protected void grid_rowediting_Desig(object sender, GridViewEditEventArgs e)
        {
            
                if (conn.State == ConnectionState.Open)
                    conn.Close();
                //string id=grid_viewdept.DataMember.
                string id = (string)grid_viewDesig.DataKeys[e.NewEditIndex].Value;
                ad = new SqlDataAdapter("select designation_name,designation_id from designation  where designation_id='" + id + "' and type='N'", conn);
                dtDesign = new System.Data.DataTable();
                ad.Fill(dtDesign);
                if (dtDesign.Rows.Count > 0)
                {

                    grid_viewDesig.DataSource = dtDesign;
                    txtDesignation_name.Text = dtDesign.Rows[0][0].ToString();
                    txtDesignation_id.Text = dtDesign.Rows[0][1].ToString();

                    btnmodify.Visible = true;
                    btn_addnew.Visible = false;
                }

            }
           

        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900