Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  The Hidden Power of the DataGrid Control - Part 1  »  Data Grid and Data Binding
 »  Home  »  Data Programming  »  The Hidden Power of the DataGrid Control - Part 1  »  Data Grid and Data Binding
 »  Home  »  Windows Development  »  Win Forms  »  The Hidden Power of the DataGrid Control - Part 1  »  Data Grid and Data Binding
The Hidden Power of the DataGrid Control - Part 1
by Dmytro Lapshyn | Published  01/13/2005 | .NET Newbie Data Programming Win Forms | Rating:
Data Grid and Data Binding

The DataGrid control relies on the data binding architecture we have outlined in the previous section. At the same time, there are some specifics worth a more detailed explanation.

We will start from types of data sources the DataGrid control can be bound to. The most popular types are of course DataTable and DataSet, which are followed by DataView. However, DataGrid is not limited to ADO .NET types and is also capable of handling single-dimension arrays as well as various kinds of lists implementing either the IList or the IListSource interface.

As you have probably noted, all these data sources share a common feature – they all can be uniformly treated as lists having a known number of items. This determines the type of binding manager the DataGrid control uses: it is the CurrencyManager class. In most cases, a single CurrencyManager instance is enough, but when a DataGrid is bound to a DataSet having multiple tables with relations established between them, multiple CurrencyManagers can be created to serve each related table. We will hereafter use the word “list” to define a sequence of items managed by a single CurrencyManager instance.

The DataGrid control exposes two properties related to its data source. The DataSource property gets or sets a data source object the DataGrid instance is bound to. As the DataGrid control is limited with working with a single list at a time, the DataSource property is complemented with the DataMember property to specify a particular list within the DataSource to work with.

The DataMember property contents may vary depending on the nature of the data source and has a generic name “navigation path”. Valid examples of navigation paths are the name of a table in a data set or the name of a relation between master and detail tables in the same data set.

When being bound to a data source, the DataGrid control first creates a CurrencyManager instance for the list specified with the DataMember property value. We will call this instance a primary CurrencyManager instance. Other instances of the CurrencyManager class can be created on demand when, for example, the user navigates a relationship between the parent and the child table within a bound DataSet.

This leads us to the following line of code that returns a CurrencyManager instance a DataGrid instance currently cooperates with:

CurrencyManager cm = this.BindingContext[ theGrid.DataSource, theGrid.DataMember];

You might be concerned at this point about the reason why one should ever bother with CurrencyManagers, and I promise you will see how important their role is.

Data Grid Navigation Mechanics

The DataGrid control maintains a notion of a current row. This row is visually marked with a little black triangle displayed in the corresponding row header. The notion of a current row is used for data editing purposes and for navigating between parent and child tables. DataGrid does not, however, track the current row by itself, relying on its CurrencyManager(s) instead as shown on Figure 2.


Figure 2

You have control over the DataGrid’s current row through the CurrentRowIndex property. This property enables you to retrieve and update current row index corresponding to the primary CurrencyManager’s position. In other words, this property reflects the position within the parent list, and a relation is navigated, the value of this property doesn’t change.

Note though that there is no property to control the position for any of the auxiliary CurrencyManagers. This limitation, however, is no more an obstacle for us, as we already know how to access the CurrencyManager instance for the current list displayed in the DataGrid.

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 01/15/2005)
Rating
The article didn't address my major question about datagrids, but it was well written, very insightful and helpful. The more information about the confounding datagrid control I can get my hands on, the better!

Thanks for the article - looking forward to the next.


 
Comment #2  (Posted by an unknown user on 02/05/2005)
Rating
very helpfull thanks !
 
Comment #3  (Posted by an unknown user on 02/07/2005)
Rating
Seems like VB.net is a great tool but the MS documentation sucks. I can see many people are not getting it yet even with good programmers trying to explain it all. VB.Net won't get going very well until the documentation is much, much better and many tutorials are written by bloodied warriors like Dmytro. Thanks for trying to help us poor mortals.
 
Comment #4  (Posted by an unknown user on 02/08/2005)
Rating
Seems like VB.net is a great tool but the MS documentation sucks. I can see many people are not getting it yet even with good programmers trying to explain it all. VB.Net won't get going very well until the documentation is much, much better and many tutorials are written by bloodied warriors like Dmytro. Thanks for trying to help us poor mortals.
 
Comment #5  (Posted by an unknown user on 03/05/2005)
Rating
It helps me easily...
 
Comment #6  (Posted by an unknown user on 03/16/2005)
Rating
I liked the way the architecture is explained. Exactly what I looked for.
 
Comment #7  (Posted by an unknown user on 03/26/2005)
Rating
The Microsoft documentation on the datagrid is terrible. They describe the millions of bits but leave it to the developer to try and figure how all the bits work together. Thanks for trying to provide a better description.
 
Comment #8  (Posted by an unknown user on 03/30/2005)
Rating
because very introductory information
 
Comment #9  (Posted by an unknown user on 03/31/2005)
Rating
May have been better if he went through an example
 
Comment #10  (Posted by an unknown user on 04/05/2005)
Rating
Good overview on the general architecture of the list Binding concept (information terribly lacking in MS documentation). If I would have found this article first, all the other stuff I found on binding grids would have made much more sense (as they do now, after reading this).
Hope to see part 2 about the presentation-side (the 'styles') very soon...
 
Comment #11  (Posted by an unknown user on 04/14/2005)
Rating
it was a silver lining otherwise datagrid really had me turned inside out
 
Comment #12  (Posted by an unknown user on 05/11/2005)
Rating
I understand the use of CurrencyManager but I have yet to figure out when I would enter a datagrid.DataMember. What DataMember as it is always null whenever I inspect the value passed? Microsoft's Help documentation is very poor on this topic. The last section of Final I had to learn on my own...that is execute datagrid.EndEdit and CurrencyManager.EndCurrentEdit.
 
Comment #13  (Posted by an unknown user on 05/28/2005)
Rating
This article is very good but i need to ask if i need to make a search for a value in the data grid .in Oracle Forms i make EnterQuery and then i typed the word which i need to search for in the cell which i wanna search in and then i ExecuteQuery then the results appears on the grid or TextBoxes .
so if i have a datagrid has 3 columns (CODE , NAME , LOC) and i wanna make a query for the "TX" in the LOC column how can i do it .......
if u have answer plz send me mail at eagle_165@hotmail.com
thanks alot

 
Comment #14  (Posted by an unknown user on 07/17/2005)
Rating
Excellent,The description was so visually critical,i can't wait for Part2.This is .Net
 
Comment #15  (Posted by an unknown user on 07/18/2005)
Rating
Like many of the other Posts I have had a tough time with the documentation. I think it was written for the people who don't need it. Thanks for explaining this subject in a way both Programmers with experience and those like myself, the ones struggling to learn can use..
Thanks again.
Rattlemouth

 
Comment #16  (Posted by an unknown user on 07/22/2005)
Rating
Because there is a lake of codeing explaination for this reeasion 50%50%
 
Comment #17  (Posted by an unknown user on 10/07/2005)
Rating
helps me solve a problem that got me stuck for days.
 
Comment #18  (Posted by an unknown user on 10/09/2005)
Rating
Excellent!!! I've been working on how to tell programatically the datatable to add the last editting row of a datagrid and you've given me the answer! This is great!Thank You!
 
Comment #19  (Posted by Mike on 10/20/2005)
Rating
very nice work on the article, but i think the images are linked incorrectly... the ones i see are screenshots of installing webparts not the datagrid... other than that excellent information you have here.
 
Comment #20  (Posted by an unknown user on 10/24/2005)
Rating
ITs great article, all my doubts are cleared. by bkvijayanand.
 
Comment #21  (Posted by aneesh on 10/29/2005)
Rating
This is very help full.But please tell me how we can add rows at runtime.
 
Comment #22  (Posted by an unknown user on 12/18/2005)
Rating
Help me solve an immediate problem with automating default values.
Many Thanks
 
Comment #23  (Posted by an unknown user on 06/30/2006)
Rating
The dbGrid is trash, but at least there is advice that is right here.
 
Comment #24  (Posted by an unknown user on 10/17/2006)
Rating
Good. But I was looking for how to display combo box in a cell which is not covered in this article.
Asim
 
Comment #25  (Posted by an unknown user on 01/04/2007)
Rating
I am not not able to understand all.
I got 1/3.
This article does't tell how to
implement this in a project.
soorajpv,calicut,kerala,india
 
Comment #26  (Posted by Mohammed Akbar on 02/12/2007)
Rating
Binding,Currency manager,Binding Context and editing logic very well explained. How ever i would like to add something here, in the final section u said we have to have tablestyle to use datagrid.endedit method, but without table style also it can used by giving null value for columnstyle and -1 for rowindex as mentioned below.
datagrid.endcurrentedit(null,-1,false)

 
Comment #27  (Posted by an unknown user on 04/02/2007)
Rating
because i have searched the world over for information on user interfaces and this brave man had the courage to call it datagrids...and because you made techical simple....as someone said for us poor mortals...why is microsoft so technical...sorry forget that
 
Comment #28  (Posted by an unknown user on 05/10/2007)
Rating
Good Aticle.Actually i need to know about Function "Column Started Editing method".Is there any way to indicate
that grid is being updated, through this method.I mean I want to set flag.
Iam not getting on what basis i should keep. Please help me.MailID=narendra.p@inteqsolutions.com

 
Comment #29  (Posted by an unknown user on 08/19/2007)
Rating
Excellent information, very well explained, altough there are no code examples but even then it is a great article, i would incourge author to provide detail relevant code examples.
 
Comment #30  (Posted by Ather on 08/19/2007)
Rating
Great article, infact one of the best articles i have ever seen on datagrid, but i would incourge auther to provide detial code examples so that it would become very easy to implement all whatever have been explained. Great work
 
Comment #31  (Posted by an unknown user on 02/12/2008)
Rating
this is useless
 
Sponsored Links