Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Windows Controls C# » DataGridView Control in C#

DataGridView Control in C#

This step by step tutorial shows how to use the Windows Forms DataGridView Control in Visual Studio 2008.

Author Rank :
Page Views : 89949
Downloads : 1115
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
DataGridView.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction

The DataGridView control available as a part of Windows Forms controls in Visual Studio 2008 is much more powerful than it's previous versions. This tutorial is a basic introduction of how to write a simple data driven application using Visual Studio 2008 wizards without writing a single line of code. In my next articles, I will talk about more detailed features of the DataGridView control.

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data. This control also let's you display data in a master-details view.

With the DataGridView control, you can display and edit tabular data from many different kinds of data sources. Binding data to the DataGridView control is straightforward and intuitive, and in many cases it is as simple as setting the DataSource property. When you bind to a data source that contains multiple lists or tables, set the DataMember property to a string that specifies the list or table to bind to.

The DataGridView control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:

  • Any class that implements the IList interface, including one-dimensional arrays.
  • Any class that implements the IListSource interface, such as the DataTable and DataSet classes.
     
  • Any class that implements the IBindingList interface, such as the BindingList<(Of <(T>)>) class.
  • Any class that implements the IBindingListView interface, such as the BindingSource class.
The DataGridView control supports data binding to the public properties of the objects returned by these interfaces or to the properties collection returned by an ICustomTypeDescriptor interface, if implemented on the returned objects. Typically, you will bind to a BindingSource component and bind the BindingSource component to another data source or populate it with business objects. The BindingSource component is the preferred data source because it can bind to a wide variety of data sources and can resolve many data binding issues automatically.

The DataGridView control can also be used in unbound mode, with no underlying data store. For a code example that uses an unbound DataGridView control, see Walkthrough: Creating an Unbound Windows Forms DataGridView Control. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. When you want your Windows Forms application to display tabular data, consider using the DataGridView control before others (for example, DataGrid). If you are displaying a small grid of read-only values, or if you are enabling a user to edit a table with millions of records, the DataGridView control will provide you with a readily programmable, memory-efficient solution.

Getting Started

OK let's get started. Follow these steps and you will see how to write a data driven application without writing a single line of code.

Step 1. Create a new Windows Forms Application project in Visual Studio 2008.

Image1.gif

Figure 1.

Step 2. Drag and drop DataGridView control from toolbox to form window.

Image2.gif

Figure 2.

Step 3. Now choose a data source by right clicking on the DataGridView and then click on Add Project Data Source. We will be adding a new data source to the project right now.

Image3.gif

Figure 3.

Step 4. Now select data source type, I am choosing Database in this example.

After selecting Database, click next.

Image4.gif

Figure 4

Step 5. Choose your data connection, if you already have a connection available select that, otherwise make new connection, and follow the steps and after that click next.

Image5.gif

Figure 5.

Step 6. Add Connection Wizard.

Image6.gif

Figure 6

Step 7. Choose you database objects, and click next, if you want local database caching then check enable local database caching check box and click next.


Image7.gif

Figure 7.

Step 8. Select table for caching.

Image8.gif

Figure 8.

Step 9. Now your data source is created, right click on DataGridView control and on the option Choose Data Source, select the data source you just created.

Image9.gif

Figure 9.

That's all. You are all set.


Now you simply need to add one line of code. If your form load event does not have this code allready. add this code. Otherwise it should have it.

private void Form1_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'vendorDataSet.Vendor' table.
    // You can move, or remove it, as needed.

   this.vendorTableAdapter.Fill(this.vendorDataSet.Vendor);
}

Step 10. Now build and run the application. Your application looks like the following.

Image10.gif

Figure 10.

Summary

In this tutorial, you saw how to build a simple data driven application using a DataGridView control in Visual Studio 2008 without writing a single line of code. 

More articles on DataGridView

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Raj Kumar

Raj Kumar is a Microsoft MVP and Senior Software Engineer with lots of hands on experience using ASP.NET 2.0/3.5, AJAX, MVC, C#, Visual Basic .NET, SQL Server 2005/2008, Oracle, WPF, WCF, XAML and Silverlight. He has over 6 years of IT experience working most on Microsoft technologies. He holds Master's degree in Computer Science. When he is not writing code, he likes to write articles and play cricket.

Reach him at raj2511984@gmail.com 

 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
asp grid viwes by harsha On September 22, 2008
how to create buttons delete and edit to every record in grid view
Reply | Email | Modify 
Re: asp grid viwes by Raj On May 13, 2009
there are property of gridview showdeletebutton and showeditbutton, make them true. or you can use TemplateColumn and put you control in itemtemplete.
Reply | Email | Modify 
bind the image in datagridview by neelesh On October 12, 2008
how to bind the image in datagridview pls send the code
Reply | Email | Modify 
Re: bind the image in datagridview by Raj On July 16, 2009

Use this column to bind images in grid view
<
asp:TemplateColumn>
<ItemTemplate>
<table>
<tr>
<td valign="top" align="center">
<img border="2" class="menimageborder" src='<%#getSRC(Container.DataItem)%>' height="50" width="50">
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>


//Used to bind GridView
  private void BindGrid()
    {
 DataSet ds = new DataSet();
 ds = p_func.GetImages();
        if (ds.Tables[0].Rows.Count > 0)
        {
           Grid.DataSource = ds;
           Grid.DataBind();
        }
        else
        {
            Grid.DataSource = null;
            Grid.DataBind();
        }
       
    }

//Used to get image physical path

public string getSRC(object imgSRC)
    {
        DataRowView dRView = (DataRowView)imgSRC;
        return ResolveUrl("~/Images/" + "/" + dRView["ImageName"].ToString());
    }

 

 

Reply | Email | Modify 
picking data by yaser On January 25, 2009
can anyone tell me how can I pick data from cells ? for example I need to pick the value from cell ( row=1 , column=2 ) thank you
Reply | Email | Modify 
Re: picking data by Raj On July 16, 2009

string str = dataSet.Tables[0].Rows[1][2].ToString();

Reply | Email | Modify 
How do I edit, delete, and update rows in the datagridview by theo On April 21, 2009
can you help me with the codes or send them to theodondre@yahoo.com
Reply | Email | Modify 
Re: How do I edit, delete, and update rows in the datagridview by Raj On July 16, 2009
what code you looking this article code or something else?
Reply | Email | Modify 
datagridview + navigator combination by Shashi Sagar On May 3, 2009
how to combination of these two object
Reply | Email | Modify 
Binding a text box to the data grid view by Michael On July 27, 2009
I am using a data grid view to basically create a list box.  I have a text box that is bound to one of the columns in the data grid so that when a user selects a row from the data grid the text box is populated with that value.  How can I make this work both ways so that if the user wanted to enter in a particular value listed in the grid then that row would be selected.  I am basically creating an input form that can be used by either mouse users or 10 key users.
Reply | Email | Modify 
couldnt find datasource Microsoft Sql Server (SqlClient) by denny On November 26, 2009
hi i want ask about steps no 6.when i click change, i couldnt find datasource Microsoft Sql Server (SqlClient), there are only 3 datasource available which is access, sql server compact 3.5 and sql server database file

how to make new datasource Microsoft Sql Server (SqlClient)? and is that working to show data from mssql server 2005 database? thanks in advance
Reply | Email | Modify 
Need to work with values from datagrid by Sudha On January 23, 2010
Hi, 

I need to work with the values entered by the user into the datagrid and display them back onto the same datagrid. I need to check the cells that are empty, and then pick the values from the cells that are not empty ( which are entered by the user) and perform some calculations and display them back onto the datagrid. I have struggling with this. Please help!! 

Thanks.
gsm
Reply | Email | Modify 
edit DataGridView by Tuck On April 1, 2010
How do you edit a column on the datagridview and when the enter key is press update the database?
Reply | Email | Modify 
comment by Roshan On November 18, 2010
Sorry boss
I want external autonumber to add to datatgridview

Reply | Email | Modify 
hi by Raja On December 7, 2010
your design datagrid in windows from.

how can do with webform.

by using c#.

with fornt end values.

i have 3 text boxes named as
eid,
ename,
join date.

and two buttons

save, clear

on entering the data in text boxes that has to display in datagrid.?

design process is completed. but i dont know the code

what to write,
where to write,
how to write.
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.