Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Easy Example to make DataGridView using c#

Posted on 2006-11-09
1
Medium Priority
?
263 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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

926 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