Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic
Article

Obtaining Current DataTable Row for a DataGrid

Rate me:
Please Sign up or sign in to vote.
4.82/5 (36 votes)
15 Feb 20052 min read 169.9K   50   22
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.

C#
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.

C#
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:

C#
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


Written By
Cyprus Cyprus
Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"

I am a former Microsoft Regional DPE (MEA) covering 85 countries, former Microsoft Regional Director, and 10 Year Microsoft MVP.

I have lived in Bulgaria, Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, The Caribbean, and USA.

Creator of Indy, IntraWeb, COSMOS, X#, CrossTalk, and more.

Comments and Discussions

 
SuggestionRe: Obtainining Current Row from a DataGridView Pin
Taofeek Lasisi19-Jun-12 6:00
professionalTaofeek Lasisi19-Jun-12 6:00 
GeneralRe: Obtainining Current Row from a DataGridView Pin
Chad Z. Hower aka Kudzu19-Jun-12 8:45
Chad Z. Hower aka Kudzu19-Jun-12 8:45 
GeneralThanks Pin
euwe2-Mar-08 20:56
euwe2-Mar-08 20:56 
GeneralThanks Pin
WillEight18-Oct-07 5:24
WillEight18-Oct-07 5:24 
GeneralThis is really helpful - Thanks Pin
billy p25-Oct-06 9:13
billy p25-Oct-06 9:13 
Generalpls help to control data grid in VB.Net 2003 Pin
deepakkp26-Sep-06 20:59
deepakkp26-Sep-06 20:59 
Generaldata grid Pin
Athar Raza Faridi13-Sep-06 4:59
Athar Raza Faridi13-Sep-06 4:59 
GeneralRe: data grid Pin
Chad Z. Hower aka Kudzu15-Sep-06 2:16
Chad Z. Hower aka Kudzu15-Sep-06 2:16 
GeneralRe: data grid Pin
papacol20-May-08 22:58
papacol20-May-08 22:58 
GeneralThanks! Pin
Mark Schumann17-Jul-06 22:56
Mark Schumann17-Jul-06 22:56 
GeneralBrilliant Pin
mark_e_mark17-Feb-06 3:17
mark_e_mark17-Feb-06 3:17 
GeneralGreat Tip, I appreciate Pin
Russell Aboobacker10-Feb-06 4:15
Russell Aboobacker10-Feb-06 4:15 
QuestionHow can I set a certain row as selected into the grid? Pin
Stanciu Vlad8-Sep-05 6:42
Stanciu Vlad8-Sep-05 6:42 
AnswerRe: How can I set a certain row as selected into the grid? Pin
Chad Z. Hower aka Kudzu8-Sep-05 7:37
Chad Z. Hower aka Kudzu8-Sep-05 7:37 
GeneralRe: How can I set a certain row as selected into the grid? Pin
Stanciu Vlad8-Sep-05 9:23
Stanciu Vlad8-Sep-05 9:23 
GeneralRe: How can I set a certain row as selected into the grid? Pin
Taofeek Lasisi19-Jun-12 6:25
professionalTaofeek Lasisi19-Jun-12 6:25 
GeneralGreat Begginers Sample Pin
sagi4413-Aug-05 4:43
sagi4413-Aug-05 4:43 
GeneralRe: Great Begginers Sample Pin
Palilo24-Nov-05 17:52
Palilo24-Nov-05 17:52 
Well, I wasted three hours, and it was really frustrating not knowing where to start from for such a simple task...
Thank you Chad!
GeneralRe: Great Begginers Sample Pin
tomylee31-May-07 10:15
tomylee31-May-07 10:15 
QuestionMultiple rows selected? Pin
Steven A Bristol16-Feb-05 0:58
Steven A Bristol16-Feb-05 0:58 
AnswerRe: Multiple rows selected? Pin
patgrape16-Feb-05 2:24
patgrape16-Feb-05 2:24 
GeneralRe: Multiple rows selected? Pin
Chad Z. Hower aka Kudzu16-Feb-05 3:00
Chad Z. Hower aka Kudzu16-Feb-05 3:00 

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

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