Solved

Easy Example to make DataGridView using c#

Posted on 2006-11-09
1
251 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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

18 Experts available now in Live!

Get 1:1 Help Now