C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Showing the data in a Datagrid from the database


Posted Date: 18 Jul 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Abhisek PandaMember Level: Gold    
Rating: 1 out of 5Points: 10



The following set of code will show how to retrieve data from a database using DataGrid control.
*****************************************************************************************************

In the code it is assumed that there is a table(Student) in MS Access database with two fields(sName & sRoll) containing some values or data.

Now design a form with a DataGrid control and a button(Show). Change the name of the button to btnShow.


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

namespace ShowData
{
public partial class Form1 : Form
{
Dataset ds;
OleDbCommand cmd;
OleDbConnection con;
OleDbDataAdapter da;
OleDbCommandBuilder cb;
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(Object Sender,EventArgs e)
{
//filling the dataadapter with data(Student is only the nameofthe
//datagrid at runtime) using the dataset
da.Fill(ds,"Student");
}
private void Form1_Load(Object Sender,EventArgs e)
{
con = new OleDbConnection("DataSource = d:\\Student.mdb;Provider = Microsoft.Jet.OleDb4.0");
cmd = new OleDbCommand("select*from student",con);
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
cb = new OleDbCommandBuilder();
cb.DataAdapter = da;
ds = new Dataset();
//binding the datagrid to the dataset
this.dataGrid1.DataSource = ds;
}
}
}


We are using DataSet here which acts as a virtual table. The value is retrieved from the database and stores in the DataSet. Then we are passing this data to the DataGrid through DataSet.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Showing Data  .  Retriving data  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Displaying various cursors and tool tip text for DataGridView columns
Previous Resource: Javascript function to select all checkboxes of grid control
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use