SIGN UP MEMBER LOGIN:    
ARTICLE

DataGridView Control in C#

Posted by Raj Kumar Articles | Windows Controls C# September 16, 2008
This step by step tutorial shows how to use the Windows Forms DataGridView Control in Visual Studio 2008.
Reader Level:
Download Files:
 

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

share this article :
post comment
 

Have you go to the <a href="http://www.longchamp-sales.net">Longchamp sales</a> today? <a href="http://www.longchamp-sales.net">Longchamp Le Pliage</a> to create a new temptation for us, <a href="http://www.longchamp-sales.net">Longchamp Bags</a> provide you with a simple breezy feeling of spring. <a href="http://www.longchamp-bags.org">Longchamp Le Pliage</a> has certainly not do not provide us surprise,and Cheap <a href="http://www.longchamp-bags.org">Longchamp bags</a>, has been owned to a youthful vitality and its power.<a href="http://www.longchamp-bags.org">Longchamp Sale</a> always gave the surprise of what you want.

Posted by xiaoyan ning Feb 28, 2012

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.

Posted by Raja Babu Dec 07, 2010

Sorry boss
I want external autonumber to add to datatgridview

Posted by Roshan Sonekar Nov 18, 2010

How do you edit a column on the datagridview and when the enter key is press update the database?

Posted by Tuck Hoh Apr 01, 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

Posted by Sudha Jan 23, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor