Click here to Skip to main content
11,415,973 members (83,620 online)
Click here to Skip to main content

Obtaining Current DataTable Row for a DataGrid

, 15 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
www.KudzuWorld.com

Formerly the Regional Developer Adviser (DPE) for Microsoft Middle East and Africa, he was responsible for 85 countries spanning 4 continents and 10 time zones. Now Chad is a Microsoft MVP.

Chad is the chair of several popular open source projects including Indy and Cosmos (C# Open Source Managed Operating System).

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. Chad has visited more than 60 countries, visiting most of them several times.

Comments and Discussions

 
SuggestionRe: Obtainining Current Row from a DataGridView Pin
Taofeek Lasisi19-Jun-12 7:00
memberTaofeek Lasisi19-Jun-12 7:00 
GeneralRe: Obtainining Current Row from a DataGridView Pin
Chad Z. Hower aka Kudzu19-Jun-12 9:45
memberChad Z. Hower aka Kudzu19-Jun-12 9:45 
GeneralThanks Pin
euwe2-Mar-08 21:56
membereuwe2-Mar-08 21:56 
GeneralThanks Pin
wbidgood18-Oct-07 6:24
memberwbidgood18-Oct-07 6:24 
GeneralThis is really helpful - Thanks Pin
billy p25-Oct-06 10:13
memberbilly p25-Oct-06 10:13 
Generalpls help to control data grid in VB.Net 2003 Pin
deepakkp26-Sep-06 21:59
memberdeepakkp26-Sep-06 21:59 
Generaldata grid Pin
atharfaridi13-Sep-06 5:59
memberatharfaridi13-Sep-06 5:59 
GeneralRe: data grid Pin
Chad Z. Hower aka Kudzu15-Sep-06 3:16
memberChad Z. Hower aka Kudzu15-Sep-06 3:16 
GeneralRe: data grid Pin
Nino Mrdjen20-May-08 23:58
memberNino Mrdjen20-May-08 23:58 
GeneralThanks! Pin
Mark Schumann17-Jul-06 23:56
memberMark Schumann17-Jul-06 23:56 
GeneralBrilliant Pin
mark_e_mark17-Feb-06 4:17
membermark_e_mark17-Feb-06 4:17 
GeneralGreat Tip, I appreciate Pin
Russell Aboobacker10-Feb-06 5:15
memberRussell Aboobacker10-Feb-06 5:15 
QuestionHow can I set a certain row as selected into the grid? Pin
Vlad Stanciu8-Sep-05 7:42
memberVlad Stanciu8-Sep-05 7:42 
AnswerRe: How can I set a certain row as selected into the grid? Pin
Chad Z. Hower aka Kudzu8-Sep-05 8:37
memberChad Z. Hower aka Kudzu8-Sep-05 8:37 
GeneralRe: How can I set a certain row as selected into the grid? Pin
Vlad Stanciu8-Sep-05 10:23
memberVlad Stanciu8-Sep-05 10:23 
GeneralRe: How can I set a certain row as selected into the grid? Pin
Taofeek Lasisi19-Jun-12 7:25
memberTaofeek Lasisi19-Jun-12 7:25 
GeneralGreat Begginers Sample Pin
sagi4413-Aug-05 5:43
membersagi4413-Aug-05 5:43 
GeneralRe: Great Begginers Sample Pin
Palilo24-Nov-05 18:52
memberPalilo24-Nov-05 18:52 
GeneralRe: Great Begginers Sample Pin
tomylee31-May-07 11:15
membertomylee31-May-07 11:15 
QuestionMultiple rows selected? Pin
Steven Bristol16-Feb-05 1:58
memberSteven Bristol16-Feb-05 1:58 
AnswerRe: Multiple rows selected? Pin
patgrape16-Feb-05 3:24
memberpatgrape16-Feb-05 3:24 
GeneralRe: Multiple rows selected? Pin
Chad Z. Hower aka Kudzu16-Feb-05 4:00
memberChad Z. Hower aka Kudzu16-Feb-05 4:00 

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.

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