Click here to Skip to main content
Licence 
First Posted 15 Feb 2005
Views 116,358
Bookmarked 48 times

Obtaining Current DataTable Row for a DataGrid

By | 15 Feb 2005 | Article
Some things in .NET are missing "practical" short cuts. This article demonstrates how to obtain the row in a DataTable that the currently selected row in a DataGrid is linked to.

Introduction

It's a shame that this requires even a short article to explain this - but some things in .NET are missing "practical" short cuts. This article demonstrates how to obtain the row in a DataTable that the currently selected row in a DataGrid is linked to.

Warning: Topic is to be considered to be of beginner level. Please no e-mail from intermediate or advanced developers about this article being "too basic". There are still many beginning programmers looking for beginner level articles - in fact once upon a time we were all there and complaining that all the articles were "too advanced".

Code

This is the basic form for obtaining the current row that is linked to the currently selected grid row. This is expressed in long form, since in the short form (consolidated into one line), of all the type casting becomes quite mangled.

using System.Data;
CurrencyManager xCM = 
  (CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember];
DataRowView xDRV = (DataRowView)xCM.Current;
DataRow xRow = xDRV.Row;

Using this form, xRow will now contain a reference to the row in the DataTable. Using xRow you can now read the raw data from the bound DataTable. If you are using typed DataSets, you will need to further type cast xRow to the typed row.

Position

Wouldn't the Position property be much simpler? Well yes, and no. You can use the Position property as an index into the DataTable rows, until a user sorts the grid by clicking on a column heading. The DataView for the grid will then not match the DataTable ordering and indexing will return the wrong records. Indexing into the DataView is possible, but it still requires many type casts to achieve.

Shortcutting

Hopefully, in a future version of WinForms, Microsoft will take note and add a CurrentRow property to the DataGrid for easy access. Until then the logical alternative is to create a custom control and add this property ourselves. While creating custom controls is not difficult, creating a custom assembly and installing it into the toolbox for one method is a bit much. Instead, you can use the following short cut.

public class Global { 
  public Global() { 
  }
  public static DataRow CurrentRow(DataGrid aGrid) {
    CurrencyManager xCM = 
      (CurrencyManager)aGrid.BindingContext[aGrid.DataSource, aGrid.DataMember];
    DataRowView xDRV = (DataRowView)xCM.Current;
    return xDRV.Row;
  }
}

This is defined as a static method, so now from your form code you can obtain the current row for a DataGrid simply by using the following example:

DataRow xRow = Global.CurrentRow(MyDataGrid);

Nothing it this article is revolutionary, or advanced. But hopefully it will help you on your journey through WinForms...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chad Z. Hower aka Kudzu

Other

Cyprus Cyprus

Member

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
Website: http://www.KudzuWorld.com
Blogspace: http://www.KudzuWorld.com/blogs/
Speaking Profile: http://www.woo-hoo.net/
 
Formerly the Regional Developer Adviser (DPE) for Microsoft MEA (Middle East and Africa), he was responsible for 85 countries spanning 4 continents and crossing 10 time zones. Now Chad is Microsoft MVP and a professional speaker at popular developer conferences worldwide. Chad was once introduced as having "mastered more languages than a United Nations translator." Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming. Chad has lived in Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, and the United States. In total Chad has visited more than 50 countries, visiting most of them many times.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks Pinmembereuwe20:56 2 Mar '08  
GeneralThanks Pinmemberwbidgood5:24 18 Oct '07  
GeneralThis is really helpful - Thanks Pinmemberbilly p9:13 25 Oct '06  
Generalpls help to control data grid in VB.Net 2003 Pinmemberdeepakkp20:59 26 Sep '06  
Generaldata grid Pinmemberatharfaridi4:59 13 Sep '06  
GeneralRe: data grid PinmemberChad Z. Hower aka Kudzu2:16 15 Sep '06  
GeneralRe: data grid PinmemberNino Mrdjen22:58 20 May '08  
GeneralThanks! PinmemberMark Schumann22:56 17 Jul '06  
GeneralBrilliant Pinmembermark_e_mark3:17 17 Feb '06  
GeneralGreat Tip, I appreciate PinmemberRussell Aboobacker4:15 10 Feb '06  
QuestionHow can I set a certain row as selected into the grid? PinmemberVlad Stanciu6:42 8 Sep '05  
AnswerRe: How can I set a certain row as selected into the grid? PinmemberChad Z. Hower aka Kudzu7:37 8 Sep '05  
GeneralRe: How can I set a certain row as selected into the grid? PinmemberVlad Stanciu9:23 8 Sep '05  
GeneralGreat Begginers Sample Pinmembersagi444:43 13 Aug '05  
GeneralRe: Great Begginers Sample PinmemberPalilo17:52 24 Nov '05  
GeneralRe: Great Begginers Sample Pinmembertomylee10:15 31 May '07  
QuestionMultiple rows selected? PinmemberSteven Bristol0:58 16 Feb '05  
AnswerRe: Multiple rows selected? Pinmemberpatgrape2:24 16 Feb '05  
GeneralRe: Multiple rows selected? PinmemberChad Z. Hower aka Kudzu3:00 16 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 Feb 2005
Article Copyright 2005 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid