Solved

Easy Example to make DataGridView using c#

Posted on 2006-11-09
1
256 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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