Solved

Easy Example to make DataGridView using c#

Posted on 2006-11-09
1
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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