Article Options
Premium Sponsor
Premium Sponsor

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

Dmytro Lapshyn works as a CTO for Validio Ukraine, which is an official partner of Validio Software, LLC. He previously worked as a programmer in a volunteer student scientific and production group "Programmist" of Kharkov Technical University of Radio-Electronics.

During more than 7 years of his programmer career Dmytro has developed various applications including desktop, client-server and Internet development. He has been working with Microsoft technologies since 1998 and has been developing with Microsoft .NET since the Beta 2 release in 2001. His primary areas of expertise are Visual Basic, ASP, COM+ and .NET.

Dmytro is 27 years old and lives in Kharkov, Ukraine. He has a Bachelors and a Masters degree in Computer Systems Security from Kharkov Technical University of Radio-Electronics.

Company Profile

Founded in 1992, Alis Software successfully operated in software development market for 5 years. In 1997 the company was acquired by Miik Ltd and became its Information Technologies Division. Three years of flourishing accompanied by the dynamic growth made the division the priority of the company business and since 2000 Miik Ltd. almost entirely proceeded to producing software, computer graphics, and Web applications in partnership with foreign and national companies for clients both in Ukraine and abroad.

In summer 2005, Information Technologies Division of MIIK Ltd. was reorganized into Validio Ukraine.

Validio Software provides outsourced software development services to high-tech companies and businesses that rely on technology. Based in Seattle, Washington, Validio's services include design, management, and implementation of complete projects using experienced development teams, as well as providing skilled development resources for customer driven projects. By maintaining staff of qualified software developers and experienced project managers in both the U.S. and Ukraine, Validio offers its clients technical expertise that is both scalable and cost effective.

 

View all articles by Dmytro Lapshyn...
Final

Now, as promised, we get back to differences in the DataGrid editing logic that take place when a new data row is added. One should be aware that not all types of data sources support adding new rows, only those which implement the IBindingList interface actually do. Therefore, we will assume that the DataGrid is bound to a DataSet or to a DataTable for our further explanation. The IBindingList interface is actually implemented by the DataView and the DataViewManager classes, so a DataView instance is implicitly created for each DataGrid’s CurrencyManager in case of a DataTable or a DataSet bound to it.

Now that the prerequisites have been discussed we can move on. When the user clicks on the new row addition area (the one marked with a small “*” icon), a new data row is created in the corresponding DataView but not yet in the DataTable the view has been created for. If we examine the RowState property for the corresponding DataRow, we find out that the data row is marked as Detached, which means the row does not belong to any DataTable yet.

The usual editing sequence is then carried out as if the new row was any other row. The next difference is introduced at the point when the new row values have been either committed or the editing have been rolled back. Upon the successful commit, the new row is actually added to the data source and its RowState is changed to “Added”. If the editing has been rolled back, the new row is completely removed from the DataView – this prevents the user from a multitude of blank rows that are added every time she clicks on the asterisk-marked area.

TIP: For DataTables and DataSets, the underlying row that has been added is marked with the Added value of its RowState property. This can be used later to determine which rows has been added by the user and should be somehow processed.

Another interesting point I would like to discuss is how editing works for a sorted DataGrid. As you might have noticed, if a cell being edited belongs to a column the grid is currently sorted by, the row “jumps” to its correct place in the sort order when the user has entered new cell value and moves off the cell. While we cannot prevent this jumping, we can determine the old index of the row before it jumps. To do that, we will subscribe to the ListChanged event of the DataView.

CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGrid.DataSource, dataGrid.DataMember];

DataView theView = (DataView)cm.List;
theView.ListChanged += ListChangedEventHandler(ListChangedHandler);

Now, within the handler, we will check the ListChangedType property to react only on ItemMoved notifications we are interested in. When such a notification arrives, we can analyze the values of the OldIndex and the NewIndex properties to determine the old row index and the new row index respectively.

private void ListChangedHandler( object sender, ListChangedEventArgs e) {
    if (e.ListChangedType == ListChangedType.ItemMoved) {
        int oldIndex = e.OldIndex;
        int oldIndex = e.NewIndex;
    }
}

And finally, a simple trick to force the DataGrid to complete current editing. This trick will be useful any time you want to complete editing programmatically – for example, when the user clicks the OK button in a dialog box. You will need to perform two actions:
Tell the DataGrid to exit editing mode and save the data.

  1. Tell the corresponding CurrencyManager to push the data back to the data source.

Calling the DataGrid’s EndEdit method carries out the first step. This method requires a reference to the DataGridColumnStyle instance for the column being edited, so you will have to create grid column styles manually – you won’t be able to access the grid column styles created by default.

The corresponding DataRow fields will have been updated by the time the DataGrid quits the editing mode, but row status won’t be changed – and if the row have just been created, it won’t be attached to any table.

The CurrencyManager’s EndCurrentEdit method should be called to complete the second step. After this call the row will have correct status and will be added to the DataTable if necessary.

Conclusion

In the first part of the article we have taken a closer look at the inner life of the data grid control and have taken a walk through the intricacies of its navigation and data editing logic. We have also written several pieces of code to add small but convenient features to the DataGrid control. At this point you might wish to start implementing your extended version of the DataGrid, and I absolutely encourage you to do so as well as I would suggest including these small features for a start.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.14285714285714 out of 5
 35 people have rated this page
Article Score45869
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