articles

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

DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW

Anonymous User46897 16-Sep-2010

In this Applicationwe 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.

DATA ACCESS FROM THE DATABASE IN 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      

DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW

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
privatevoid Show_Load(object sender, EventArgs e)
        {
//connection creating and passing connection string inside.
            SqlConnection con = newSqlConnection("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 = newSqlDataAdapter("select * from employee", con);
//creating datatable object
            DataTable dt = newDataTable();
            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

privatevoid 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 
publicint 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 
publicpartialclassShow1 : Form
    {
        int id;//global assign id
        public Show1()
        {
            InitializeComponent();
        }
 
        privatevoid Show_Load(object sender, EventArgs e)
        {
            SqlConnection con = newSqlConnection("server=abc-PC1\\sqlexpress; uid=sa; password=sa; database=Mind");
            con.Open();
            SqlDataAdapter ada = newSqlDataAdapter("select * from employee", con);
            DataTable dt = newDataTable();
            ada.Fill(dt);
            dataGridView1.DataSource = dt;
        }
 
        privatevoid dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            id = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
            this.Close();
         
        }
 
        publicint 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. 

publicpartialclassForm1 : Form
    {
        int nID;//gloabal declaration
        SqlConnection con;//global declaration
      
        public Form1()
        {
            InitializeComponent();
        }
 
        privatevoid button1_Click(object sender, EventArgs e)
        {
            Show1 sh = newShow1();//creating object of “show1”
           
            sh.ShowDialog();
            nID = sh.id_val;//setting the property
            SqlCommand cmd = newSqlCommand("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
        }
 
        privatevoid 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 = newSqlCommand("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);
            }
       
        }
 
 
 
        privatevoid btnClose_Click(object sender, EventArgs e)
        {
            this.Close();//close the appliaction
        }

 

       The desired Output as shown below.


DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW

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.

DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW



Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By