Solved

Easy Example to make DataGridView using c#

Posted on 2006-11-09
1
253 Views
Last Modified: 2012-05-05
Hi,

I am using C# and making Windows Application in .Net 2005

I want to fetch few records from different table and show it in the GridView

Can anyone give me short and easy example or Code which I can use it.


Thanks,
Dinesh
0
Comment
Question by:dinesh_bali
1 Comment
 
LVL 6

Accepted Solution

by:
so3 earned 250 total points
ID: 17918357
Bellow is an example on how to fetch some records from a database access and show in datagridview.
Here is the database link http://www.getquotes-it.com/Articles/DataGridViewDB.zip,  download it and put in directory where is the executable file(In BIN\DEBUG)

Here is a tutorial on how to use datagridview.
http://www.getquotes-it.com/Articles/DataGridView-Tutorial-CSharp.htm



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.OleDb;

#endregion

namespace DataGridView_Sample
{
    partial class Form1 : Form
    {
        OleDbConnection dbConn = null;
        OleDbDataAdapter dbDA = null;
        DataSet ds1 = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }
        ~Form1()
        {
            if (dbConn.State != ConnectionState.Closed)
            {
                dbConn.Close();
            }
        }

        protected virtual OleDbCommand getSelectCMD()
        {
            OleDbCommand dbCommand = new OleDbCommand();
            dbCommand.Connection = dbConn;
            dbCommand.CommandText = "SELECT PersonID, PTitle, PName FROM People";
            return dbCommand;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DataGridViewDB.mdb";

            try
            {
                dbConn = new OleDbConnection(strConn);
                dbConn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            try
            {

                dbDA = new OleDbDataAdapter(getSelectCMD());
                dbDA.Fill(ds1, "results");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            this.dataGridView1.DataSource = ds1;
            this.dataGridView1.DataMember = "results";
            this.dataGridView1.Columns[0].Visible = false;
        }
    }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now