Getting the selection in a DataGrid

Posted on 2006-04-09
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

    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

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now