Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> ADO.Net >> DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW
DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW
DATA ACCESS FROM THE DATABASE IN DATAGRIDVIEW


by Haider M Rizvi on 9/16/2010 8:48:07 PM

Views: 15803       Comments: 2

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

Report Abuse Form
Reason:    
 


DataGridView
by John Smith 3/25/2011 4:38:44 AM
Really useful Article Haider!
Report Abuse

Thanks for sharing..
by mohan kumar 10/20/2011 12:00:58 AM
Really worth reading your article...Thanks for sharing haider...
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Haider M RizviRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2738
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.