In this application we have to access data from the SQL database in the form of Gridview, and update Records while selecting Records from the gridview in another Form with update Button.

First step open new project and named as in this application like DataGridview_Application.

Create a New Form with one Gridview Control as shown below with the help of arrow and one Close Button as you can see below in the snapshot.

Form with Gridview Control

   private void Update_Load(object sender, EventArgs e)
//Creating Connection with server name,uid,password and database.
        SqlConnection con = new SqlConnection("server=abc-pc1\\sqlexpress; uid=sa; password=sa; database=Mind");
           //connection is open
       //Creating object for dataAdapter    
SqlDataAdapter sda = new SqlDataAdapter("select * from Employee", con);
//craeting datatable object
            DataTable dt = new DataTable();
//accessing data in grid
            dataGridView1.DataSource = dt;
        } }
Output of the this code look likes as:


//then double click on the close button place a code for closing the application


Next step is to access rows in the gridview in another form as we have created another form named “New Form” as shown below,for adding another form in the same application you have to Rightclick on the datagridview_Application option on the right side of the application and right click over there then click on add items as shown below and named NewForm.

New Windows Form-Data Grid

Next we have to show fields on the event of “cellclick “ option in the gridview ,for that under properties option you will find an option Event under that you will see an option “cellClick” then double click on that option  and  place a code .

   private void dataGridView1_CellDoubleClick(object sender,  DataGridViewCellEventArgs e)
//For accessing data as rows with respected cells
            int id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
       //Creating object for NewForm as nf  
         NewForm nf = new NewForm(id);
          //Display it into another form


Then Design a NewForm as shown Below in the snapshot.

New form design - Data GridView

    //how to show data in the respected fields while click on the datagrid a code under newForm Load Event as below.
//calling Global items
        SqlConnection con;
        SqlDataReader rdr;
        SqlDataReader rdr1;
        int nID;
   public NewForm(int ID)//calling constructors with one parametres as ID.
            con = new SqlConnection("server=abc-pc1\\sqlexpress; uid=sa; password=sa; database=Mind");
            nID = ID;
private void NewForm_Load(object sender, EventArgs e)
//Accessing commands from the database
            SqlCommand cmd = new SqlCommand("select * from Employee where Employee_id=" + nID, con);
//accepting it into datareader
            rdr = cmd.ExecuteReader();
            string a = rdr[0].ToString();
            txtID.Text = a;
            //taking itinto textfileld from the gridview
            txtName.Text = rdr[1].ToString();
            txtAddress.Text = rdr[2].ToString();
            txtPhoneno.Text = rdr[3].ToString();
            txtage.Text = rdr[4].ToString();
            rdr.Close();        }


Next step is to update rows from the new Form as shown in the snapshot as soon

as we click on any of the fields in the GridView u will see an another form will open

as shown below in the snapshot. Then we click on the update button and update

the records in the database.


update rerord in database through GridView


Code for update the Records as shown below.
     private void btbUpdate_Click(object sender, EventArgs e)
//show a messaage box wether we want to update a record or not.
                if (MessageBox.Show("Do you want to update record?", "update", MessageBoxButtons.YesNo) == DialogResult.Yes)
//creating a command with the update query
                    SqlCommand cmd = new SqlCommand("UPDATE employee SET Employee_id='" + txtID.Text + "',Employee_name='" + txtName.Text + "',Employee_address='" + txtAddress.Text + "',Employee_phoneno='" + txtPhoneno.Text + "',Employee_age='" + txtage.Text + "'where Employee_id='" + nID + "'", con);
                    rdr1 = cmd.ExecuteReader();
            catch (Exception ex)

Update Record from Data Grid

  Modified On Nov-17-2017 06:48:04 AM

Leave Comment