Home > DeveloperSection > Articles > DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW

DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW


ADO.Net ADO.Net 
Ratings:
2 Comment(s)
 26172  View(s)
Rate this:

DATA ACCESS FROM DATABASE IN DATAGRIDVIEW (APPLICATION)


In this Application we have to show how data is populating from the database in Datagridview on the click of a button and records display from the database in Datagridview and as we click on anyfields in the datagrid data will be shown on the same page where we are clicking the button for showing records from the Datagridview not on the another form as shown below.


form to display record from DataGridView


as we click on the Button as shown above in the figure (…),Datagridview display with their records from the database as shown below and as you select the data from the datagridview data can be display in the “form1” as shown above.For this we have to create one form with the help of Add NewForm  and      

DataGrid

Take a Datagridview inside the “Form1” as shown above with respected fields as five Textboxes along with their label and two Button one for open the Datagridview and one button for close the button.

First step is to show data from the database in the form “show” in Datagridview.

//Code

private void Show_Load(object sender, EventArgs e)

        {

//connection creating and passing connection string inside.

            SqlConnection con = new SqlConnection("server=abc-PC1\\sqlexpress; uid=sa; password=sa; database=Mind");

            con.Open();//open the connection

//seeting query in the dataadapter by creating their object

            SqlDataAdapter ada = new SqlDataAdapter("select * from employee", con);

//creating datatable object

            DataTable dt = new DataTable();

            ada.Fill(dt);//filling data in adapter

            dataDataView1.DataSource = dt; ;//setting datasource of dataDataview to object of datatable

 

        }

The above code display a record from the database in Dataview on the click event of button.

As we populate data from the database in Datagridview on selecting of the fields in the Datagridview records will be display on the “Form1” along with their fields.this would be performed by set and get property.

Place a code on the show area of button as below

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

        {

//setting various records in gridview as rowindex with their respected values in “id”

            id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);

            this.Close();

         

        }

 

Code for property

 

public int id_val //name of the property is id_value

        {

            Get  //get keyword used for value.

            {

                return (id);//passing id

            }

        }

 

Complete code can be written as follows below

 

public partial class Show1 : Form

    {

        int id;//global assign id

        public Show1()

        {

            InitializeComponent();

        }

 

        private void Show_Load(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("server=abc-PC1\\sqlexpress; uid=sa; password=sa; database=Mind");

            con.Open();

            SqlDataAdapter ada = new SqlDataAdapter("select * from employee", con);

            DataTable dt = new DataTable();

            ada.Fill(dt);

            dataGridView1.DataSource = dt;

        }

 

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

        {

            id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);

            this.Close();

         

        }

 

        public int id_val

        {

            get

            {

                return (id);

            }

        }

 

       

    }

Next we need to populate data from the various texts in “form1” on the selecting keyword of data in grid.

Complete code as shown below, place it on the various events a button click and form load event.

 

public partial class Form1 : Form

    {

        int nID;//gloabal declaration

        SqlConnection con;//global declaration

      

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            Show1 sh = new Show1();//creating object of “show1”

           

            sh.ShowDialog();

            nID = sh.id_val;//setting the property

            SqlCommand cmd = new SqlCommand("select * from employee where Employee_id='" + nID + "'", con);

//creating datareader object

              SqlDataReader rdr = cmd.ExecuteReader();

                      

                rdr.Read();

//assigning respected values from the textbox

                txtID.Text = rdr[0].ToString();

                txtName.Text = rdr[1].ToString();

                txtAddress.Text = rdr[2].ToString();

                txtPhoneno.Text = rdr[3].ToString();

                txtAge.Text = rdr[4].ToString();

                rdr.Close();//close the reader

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            try

            {

//coonection created with the help of connection string

                 con = new SqlConnection("server=abc-PC1\\sqlexpress; uid=sa; password=sa; database=Mind");

                con.Open();//open the connection

                SqlCommand cmd = new SqlCommand("select * from employee where Employee_id='" + nID + "'", con);

                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())

                {

//assigning data in various textfield.

                    txtID.Text = rdr[1].ToString();

                    txtName.Text = rdr[2].ToString();

                    txtAddress.Text = rdr[3].ToString();

                    txtPhoneno.Text = rdr[4].ToString();

                    txtAge.Text = rdr[5].ToString();

                 

                    con.Close();//closing the connection

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

       

        }

 

 

 

        private void btnClose_Click(object sender, EventArgs e)

        {

            this.Close();//close the appliaction

        }

 

      

    The desired Output as shown below.

 

GridView, Desired output

Once we click on the button as shown above it will display records in a datagridview. Next if a user Double Click any of the fields in the datagridview data will reflect in the “form1”, as shown below.

Record from DataGrid to form


DataGridView

By John Smith on   5 years ago
Really useful Article Haider!

Thanks for sharing..

By mohan kumar on   5 years ago
Really worth reading your article...Thanks for sharing haider...

Don't want to miss updates? Please click the below button!

Follow MindStick