---
title: "ACCESSING DATA FROM DATABASE IN GRIDVIEW IN C# .NET"  
description: "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"  
author: "Amit Singh"  
published: 2010-09-15  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/136/accessing-data-from-database-in-gridview-in-c-sharp-dot-net  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 4 minutes  

---

# ACCESSING DATA FROM DATABASE IN GRIDVIEW IN C# .NET

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](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/02ac0187-52f0-49f6-97cc-8cea6254b60e.png)

```
   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              con.Open();       //Creating object for dataAdapter             SqlDataAdapter sda = new  SqlDataAdapter("select * from Employee", con);       //craeting datatable object            DataTable dt = new DataTable();            sda.Fill(dt);         //accessing data in grid            dataGridView1.DataSource = dt;          } }
```

##### Output of the this code look likes as:

![GridView](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/e78478c0-8645-404e-ad5d-84f714e62de0.png)

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

this.Close();

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](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/b8b2b459-c877-4b0e-8833-f2a99d71ee57.png)

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            nf.ShowDialog();}  
```

Then Design a NewForm as shown Below in the snapshot.

![New form design - Data GridView](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/2bc8c6a9-4047-485d-a590-92a40e666d5e.png)

```
    //how to show data in the respected fields while click on the datagrid cell.place 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.    {            InitializeComponent();            con = new  SqlConnection("server=abc-pc1\\sqlexpress; uid=sa; password=sa; database=Mind");            nID = ID;}      private void NewForm_Load(object sender, EventArgs e)         {            con.Open();          //Accessing commands from the database            SqlCommand cmd = new SqlCommand("select * from Employee where Employee_id=" + nID, con);         //accepting it into datareader            rdr = cmd.ExecuteReader();            rdr.Read();            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](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/f994ebfe-e470-4da7-9db8-d2227ea5e5a2.png)

##### Code for update the Records as shown below.

```
     private void btbUpdate_Click(object sender, EventArgs e)         {            try            {               //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();                    con.Close();                    this.Close();                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }
```

\

![Update Record from Data Grid](https://www.mindstick.com/mindstickarticle/924bee57-80b0-4ec7-8cdb-24207ed8d7e9/images/5812d4a3-0dd6-4956-b409-af1a699a9f96.png)

---

Original Source: https://www.mindstick.com/articles/136/accessing-data-from-database-in-gridview-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
