ARTICLE

Display data in a Data Grid

Posted by Mahesh Chand Articles | ADO.NET in C# January 22, 2001
The sample example show you how to use Data Grid to display data in a grid format.
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.

data_g7.gif

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.

data_g6.jpg

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
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
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts