?
Solved

Getting the selection in a DataGrid

Posted on 2006-04-09
2
Medium Priority
?
177 Views
Last Modified: 2010-04-16
Greetings,

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.

Thanks,
Bob
0
Comment
Question by:ba272
1 Comment
 
LVL 13

Accepted Solution

by:
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;
          dtbData.Rows.Add(drow);
     }
     grid.DataSource = dtbData;
}
private void grid_DoubleClick(object sender, System.EventArgs e)
{
     try
     {
          // 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
          MessageBox.Show(cm.Position.ToString());
          // Data Row
          MessageBox.Show(drv.Row["ID"].ToString());
     }
     catch (Exception ex)
     {
          MessageBox.Show(ex.ToString());
     }
}
0

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