SIGN UP MEMBER LOGIN:    
ARTICLE
Reader Level:

After writing programs for 5 years, you have ever try to look at your first program when you start learning simple programs in Pascal or C++?? I wrote this simple program in early days of C# and ADO.NET.

In visual C#, the DataGrid is one of the ToolBox controls. DataGrid can be used to display data from a database by writing only few lines of code. In this article, I have used a database called "C:\\mcb.mdb" which has a table called 'Student'.

Create a Windows Application and add DataGrid to the Form

Add a DataGrid control to the form and set properties according to your needs.

Adding Source Code

Now you can add this few lines of code anywhere you want to load the data from the database. It may be mouse button click or the Form load event handler.

private void Form1_Load(object sender, System.EventArgs e)
{
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM Student","Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\mcb.mdb" );
DataSet ds = new DataSet();
da.Fill(ds, "Student");
dataGrid1.DataSource = ds.Tables["Student"].DefaultView ;
}

Simple. I create an OleDb DataAdapter and a DataSet object and call Fill method of data adapter, which fills the data set. After that I bind data set with the DataGrid using DataSource property. The contents of DataGrid looks like the following figure.

How to Run?

  • Download the database zip files and unzip it.
  • Add using System.Data.OleDb; namespace in your project.
    Change the database path  in this string.  "Provider=Microsoft.JET.OLEDB.4.0;data source=C:\\mcb.mdb");
  • Run the application.

Login to add your contents and source code to this article
share this article :
post comment
 

data in datagrid is not displaying and no error message is given. i need it urgently.

Posted by Sanjay Srivastava Oct 13, 2010

how to insert data to the data grid from a vb 6  windows form; pls help
thnx in advance

Posted by udara samaraweera Jun 26, 2010

i have connect the database using connect.aspx.cs and retrieving all the data from tables in this files but now i want to display this data using connect.aspx can you tell me me how to do it i am new to asp.net

Posted by Rehu pandey May 28, 2010

Manoj,
It looks like this is a almost 10 years old article. I am not sure if this same code will work in latest Visual Studio 2010. What version of Visual Studio are you using?

Try to place DataGrid control on your Form.

Then use Form load code, change your database name, and SQL statement.

Hopefully it should work.

Posted by Mahesh Chand Feb 16, 2010

where is the zip file?

Posted by manoj singhal Feb 16, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor