Getting the selection in a DataGrid

Posted on 2006-04-09
Medium Priority
Last Modified: 2010-04-16

I'm trying to get the DataRow associated with the selection in a DataGrid.  I use a DataSet as the DataSource, and I'd like to get both the row index and the DataRow, if anyone knows how to get this.

Question by:ba272
1 Comment
LVL 13

Accepted Solution

dungla earned 2000 total points
ID: 16414800
Hi ba272,

ou can use CurrencyManager class to do this. This is my sample code:

DataTable dtbData = new DataTable("New");
private void Form1_Load(object sender, System.EventArgs e)
     dtbData.Columns.Add(new DataColumn("ID", typeof(int)));
     for (int i = 1; i <= 4; i++)
          DataRow drow = dtbData.NewRow();
          drow["ID"] = i;
     grid.DataSource = dtbData;
private void grid_DoubleClick(object sender, System.EventArgs e)
          // gets binding context of data source
          CurrencyManager cm = (CurrencyManager)this.BindingContext[dtbData];
          // get current row or selected row
          DataRowView drv = (DataRowView)cm.Current;
          // index of row
          // Data Row
     catch (Exception ex)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question